Topic: Controller test fails on get_new

Hi all,

I'm new to rails so sorry if I'm asking something simple but I'm stuck on this.

When I try to test a particular controller that forms part of a one to many relationship with a users table created by authlogic I get the following error for the get_new test

test_should_get_new(BusinessesControllerTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

I had the same error when adding a new business through the page itself and on following advise from Dana on the site I added the following to my new method in the actual business controller

@business = Business.new(:user_id => current_user.id)

However I can't figure out how to implement this in the testing side of things, I've tried to edit the get_new test to the following

test "should get new" do
    get(:new,{'user_id'=>2})
    assert_response :success
  end

but to no avail can anyone give me a few pointers on this.


Thanks,


IG

Re: Controller test fails on get_new

Hi IrishGraham,

A common mistake in controller tests is to forget logging in a a user. This is usually done by stubbing the controller's "current_user" method before calling the action. In RSpec that would look like this:

it "should respond with success" do
  controller.stub(:current_user => stub('user', :id => 1)
  get :new, :user_id => 2
  response.should be_success
end

I'm sure there is a way to express this in the testing framework you're using.

Hope this helps, Henning