I want test case in rspec for: @drugs = Drug.where(''drug_category_id = ?'', params[:id]) Has anybody idea about it? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 20, 2010, at 11:34 PM, Ishit Parikh wrote:> I want test case in rspec for: > @drugs = Drug.where(''drug_category_id = ?'', params[:id]) > > Has anybody idea about it?describe Drug it "should really be posted to the rSpec Google Group :)" it "would be helpful for you to read the Prag. Programmers rSpec book" it "find Drugs if they exist" do Drug.where(''drug_category_id = ?'', params[:id]).should have(1).record end end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Steve Ross wrote in post #956143:> On Oct 20, 2010, at 11:34 PM, Ishit Parikh wrote: > >> I want test case in rspec for: >> @drugs = Drug.where(''drug_category_id = ?'', params[:id])Assuming this line is in a controller action...> it "find Drugs if they exist" do > Drug.where(''drug_category_id = ?'', params[:id]).should > have(1).record > endThe above is not how you want to write that spec. Do something like this instead: -------------------------- describe DrugsController do it "assigns the drugs within a given category as @drugs" do Drug.stub!(:where).with([ ''drug_category_id = ?'', "37" ]).and_return([ mock_drug ]) get :search, :id => "37" assigns[:drugs].should equal([ mock_drug ]) end private def mock_drug(stubs={}) @mock_drug ||= mock_model(Drug, stubs) end end -------------------------- Note: I think the syntax for assigns has changed somewhat in RSpec 2.0 so you may want to read about that change. What I''ve shown here is RSpec 1.x syntax. You don''t need to hit the database just to make sure your controller is assigning the @drugs instance variable. Use a mock instead. You don''t need to test the Rails "where()" method. Hopefully, that already has its own tests in Rails. If it were broken, it doesn''t do you any good to prove it''s broken in your application specs anyway. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote in post #956169:> Steve Ross wrote in post #956143: >> On Oct 20, 2010, at 11:34 PM, Ishit Parikh wrote: >> >>> I want test case in rspec for: >>> @drugs = Drug.where(''drug_category_id = ?'', params[:id]) > > Assuming this line is in a controller action......then you shouldn''t be writing RSpec specs for it at all. Use Cucumber instead. [...]> You don''t need to hit the database just to make sure your controller is > assigning the @drugs instance variable. Use a mock instead.It''s usually easier and more reliable to use a factory. Yes, that hits the DB. If you care that much, use an in-memory DB or something for testing.> You don''t > need to test the Rails "where()" method. Hopefully, that already has its > own tests in Rails. If it were broken, it doesn''t do you any good to > prove it''s broken in your application specs anyway.Yup! Test your use of the framework. Don''t test the framework itself. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote in post #956174:> Robert Walker wrote in post #956169: >> Steve Ross wrote in post #956143: >>> On Oct 20, 2010, at 11:34 PM, Ishit Parikh wrote: >>> >>>> I want test case in rspec for: >>>> @drugs = Drug.where(''drug_category_id = ?'', params[:id]) >> >> Assuming this line is in a controller action... > > ...then you shouldn''t be writing RSpec specs for it at all. Use > Cucumber instead.Agreed, but the OP asked about RSpec, so I showed an RSpec example.> [...] >> You don''t need to hit the database just to make sure your controller is >> assigning the @drugs instance variable. Use a mock instead. > > It''s usually easier and more reliable to use a factory. Yes, that hits > the DB. If you care that much, use an in-memory DB or something for > testing.Agreed, but was attempting to show "pure" RSpec rather than RSpec plus something else. Also, as you said using mocks can take more setup, but they can also provide a performance boost for specs that don''t actually require hitting a database. I''ll have to put some more thought in to using an in-memory database for testing. That''s intriguing. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote in post #956180:> Marnen Laibow-Koser wrote in post #956174: >> Robert Walker wrote in post #956169: >>> Steve Ross wrote in post #956143: >>>> On Oct 20, 2010, at 11:34 PM, Ishit Parikh wrote: >>>> >>>>> I want test case in rspec for: >>>>> @drugs = Drug.where(''drug_category_id = ?'', params[:id]) >>> >>> Assuming this line is in a controller action... >> >> ...then you shouldn''t be writing RSpec specs for it at all. Use >> Cucumber instead. > > Agreed, but the OP asked about RSpec, so I showed an RSpec example.OK, fair enough. [...]> Agreed, but was attempting to show "pure" RSpec rather than RSpec plus > something else. Also, as you said using mocks can take more setup,Yeah, to the point where I think they''re more trouble than they''re worth, and may actually be dangerous for AR models.> but > they can also provide a performance boost for specs that don''t actually > require hitting a database. I''ll have to put some more thought in to > using an in-memory database for testing. That''s intriguing.The idea is not original with me (I''m not such a performance weenie that I''d think of that myself :) ). SQLite can operate in in-memory mode, which I think is where the idea came from. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 21, 2010, at 1:48 PM, Robert Walker wrote:> Steve Ross wrote in post #956143: >> On Oct 20, 2010, at 11:34 PM, Ishit Parikh wrote: >> >>> I want test case in rspec for: >>> @drugs = Drug.where(''drug_category_id = ?'', params[:id]) > > Assuming this line is in a controller action... > >> it "find Drugs if they exist" do >> Drug.where(''drug_category_id = ?'', params[:id]).should >> have(1).record >> end > > The above is not how you want to write that spec. > > Do something like this instead: > -------------------------- > describe DrugsController do > it "assigns the drugs within a given category as @drugs" do > Drug.stub!(:where).with([ ''drug_category_id = ?'', "37" > ]).and_return([ mock_drug ]) > get :search, :id => "37" > assigns[:drugs].should equal([ mock_drug ]) > end > > private > def mock_drug(stubs={}) > @mock_drug ||= mock_model(Drug, stubs) > end > end > -------------------------- > Note: I think the syntax for assigns has changed somewhat in RSpec 2.0 > so you may want to read about that change. What I''ve shown here is RSpec > 1.x syntax. > > You don''t need to hit the database just to make sure your controller is > assigning the @drugs instance variable. Use a mock instead. You don''t > need to test the Rails "where()" method. Hopefully, that already has its > own tests in Rails. If it were broken, it doesn''t do you any good to > prove it''s broken in your application specs anyway.My point is, although everyone on this thread has made excellent contributions, the question really belongs in the rSpec group. The OP''s question was "I want a test case in rSpec for: @drugs = Drug.where(''drug_category_id = ?'', params[:id])" but doesn''t specify what about that is to be tested. I don''t know the OP, but suspect that the rSpec book would be a great introduction for designing tests that make sense and add robustness. My response was, admittedly, a bit tongue in cheek. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote in post #956169:> Steve Ross wrote in post #956143: >> On Oct 20, 2010, at 11:34 PM, Ishit Parikh wrote: >> it "find Drugs if they exist" do >> Drug.where(''drug_category_id = ?'', params[:id]).should >> have(1).record >> end > > The above is not how you want to write that spec. > > Do something like this instead: > -------------------------- > describe DrugsController do > it "assigns the drugs within a given category as @drugs" do > Drug.stub!(:where).with([ ''drug_category_id = ?'', "37" > ]).and_return([ mock_drug ]) > get :search, :id => "37" > assigns[:drugs].should equal([ mock_drug ]) > end > > private > def mock_drug(stubs={}) > @mock_drug ||= mock_model(Drug, stubs) > end > end > --------------------------The above code gives failure on: get :search, :id => "37" undefined method `abstract_class?'' for Object:Class -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ishit Parikh wrote in post #956228:>> Do something like this instead: >> -------------------------- >> describe DrugsController do >> it "assigns the drugs within a given category as @drugs" do >> Drug.stub!(:where).with([ ''drug_category_id = ?'', "37" >> ]).and_return([ mock_drug ]) >> get :search, :id => "37" >> assigns[:drugs].should equal([ mock_drug ]) >> end >> >> private >> def mock_drug(stubs={}) >> @mock_drug ||= mock_model(Drug, stubs) >> end >> end >> -------------------------- > > The above code gives failure on: > get :search, :id => "37" > undefined method `abstract_class?'' for Object:ClassWell, I didn''t actually run that code. I just reworked a bit of working code from one of my old projects. I''ll leave it as an exercise for you to figure out what I did wrong. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
All right. Thanx for the help... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.