Testing a scoped find in a Rails controller with RSpec

Posted by Joseph DelCioppio on Stack Overflow See other posts from Stack Overflow or by Joseph DelCioppio
Published on 2010-03-08T02:59:50Z Indexed on 2010/03/08 9:21 UTC
Read the original article Hit count: 456

Filed under:
|
|
|
|

I've got a controller called SolutionsController whose index action is different depending on the value of params[:user_id]. 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.

Here it is:

def index
    if(params[:user_id])
      @solutions = @user.solutions.find(:all)
    else
      @solutions = Solution.find(:all)
    end
  end

and @user is determined like this:

private

def load_user
   if(params[:user_id ])
    @user = User.find(params[:user_id])
   end
end

I've got an Rspec test to test the index action if the user is nil:

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

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?

Something like:

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 => @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 => @user.id
    end
  end
end

But that doesn't work. Can someone help me out?

Joe

© Stack Overflow or respective owner

Testing a scoped find in a Rails controller with RSpec

Posted by Joseph DelCioppio on Stack Overflow See other posts from Stack Overflow or by Joseph DelCioppio
Published on 2010-03-08T02:59:50Z Indexed on 2010/03/08 3:02 UTC
Read the original article Hit count: 456

Filed under:
|
|
|

I've got a controller called SolutionsController whose index action is different depending on the value of params[:user_id]. 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.

Here it is:

def index
    if(params[:user_id])
      @solutions = @user.solutions.find(:all)
    else
      @solutions = Solution.find(:all)
    end
  end

and @user is determined like this:

private

def load_user
   if(params[:user_id ])
    @user = User.find(params[:user_id])
   end
end

I've got an Rspec test to test the index action if the user is nil:

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

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?

Something like:

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 => @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 => @user.id
    end
  end
end

But that doesn't work. Can someone help me out?

Joe

© Stack Overflow or respective owner

Related posts about rspec

Related posts about controller