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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.