Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting a scoped find in a Rails controller with RSpec
    primarykey
    data
    text
    <p>I've got a controller called SolutionsController whose index action is different depending on the value of <code>params[:user_id]</code>. If its nil, then it simply displays all of the solutions on my site, but if its not nil, then it displays all of the solutions for the given user id.</p> <p>Here it is:</p> <pre><code>def index if(params[:user_id]) @solutions = @user.solutions.find(:all) else @solutions = Solution.find(:all) end end </code></pre> <p>and <code>@user</code> is determined like this:</p> <pre><code>private def load_user if(params[:user_id ]) @user = User.find(params[:user_id]) end end </code></pre> <p>I've got an Rspec test to test the index action if the user is nil:</p> <pre><code>describe "GET index" do context "when user_id is nil" do it "should find all of the solutions" do Solution.should_receive(:find).with(:all).and_return(@solutions) get :index end end end </code></pre> <p>however, can someone tell me how I write a similar test for the other half of my controller, when the user id isn't nil?</p> <p>Something like:</p> <pre><code>describe "GET index" do context "when user_id isn't nil" do before(:each) do @user = Factory.create(:user) @solutions = 7.times{Factory.build(:solution, :user =&gt; @user)} @user.stub!(:solutions).and_return(@solutions) end it "should find all of the solutions owned by a user" do @user.should_receive(:solutions).and_return(@solutions) get :index, :user_id =&gt; @user.id end end end </code></pre> <p>But that doesn't work. Can someone help me out?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload