views:

380

answers:

3

I've got two models:

class Solution < ActiveRecord::Base
  belongs_to :user

  validates_attachment_presence :software
  validates_presence_of :price, :language, :title
  validates_uniqueness_of :software_file_name, :scope => :user_id

  has_attached_file :software
end


class User < ActiveRecord::Base
  acts_as_authentic
  validates_presence_of :first_name, :last_name, :primary_phone_number
  validates_uniqueness_of :primary_phone_number

  has_many :solutions
end

with my routes looking like this:

map.resources :user, :has_many => :solutions

Now I'm trying to test my solutions controllers with the following RSpec test:

describe SolutionsController do

  before(:each) do
    @user = Factory.build(:user)
    @solution = Factory.build(:solution, :user => @user)
  end

 describe "GET index" do
   it "should find all of the solutions owned by a user" do
     Solution.should_receive(:find_by_user_id).with(@user.id).and_return(@solutions)
     get :index, :id => @user.id
   end
 end
end

However, this gets me the following error:

ActionController::RoutingError in 'SolutionsController GET index should find all of the solutions owned by a user'
No route matches {:id=>nil, :controller=>"solutions", :action=>"index"}

Can anybody point me in how I can test this, since the index should always be called within the scope of a particular user?

Thanks,

Joe

+1  A: 

Factory#build builds an instance of the class, but doesn't save it, so it doesn't have an id yet.

So, @user.id is nil because @user has not been saved.

Because @user.id is nil, your route isn't activated.

try using Factory#create instead.

  before(:each) do
    @user = Factory.create(:user)
    @solution = Factory.create(:solution, :user => @user)
  end
BaroqueBobcat
Ok, that got my closer, but now I just get: ActionController::RoutingError in 'SolutionsController GET index should find all of the solutions owned by a user' No route matches {:id=>848, :controller=>"solutions", action=>"index"}
Joseph DelCioppio
chrisdinn's answer has the solution for that problem.
BaroqueBobcat
A: 

Ok, but now that gets me:

ActionController::RoutingError in 'SolutionsController GET index should find all of the solutions owned by a user'
No route matches {:id=>848, :controller=>"solutions", :action=>"index"}
Joseph DelCioppio
A: 

Looks like your other problem is on this line:

 get :index, :id => @user.id

You're trying to make a request to the index method, but you've provided the wrong variable name. When testing SolutionsController id implies a solution id, you need to supply the user id. This should work, or at least move you forward:

 get :index, :user_id => @user.id
chrisdinn