Reputation: 339
I am testing my employees_controller, and in my index test for signed-in users, I am getting an error that seems to be related to my model relationships.
The test is:
describe "for signed-in user" do
before(:each) do
@user = test_sign_in(Factory(:user))
end
it "should have the right title" do
get :index
response.should have_selector('title', :content => "Employee List")
end
I have render_views set. I also have each user tied to a location, and the employee index view is supposed to show employees for the user's location. And in my index view, I have the following code to show the name of the location of the employees:
<h1>Listing employees for <%= current_user.location.name %> </h1>
When I run my test, I get an error message that contains "ActionView::Template::Error: undefined method 'name for nil:NilClass"
In my factories.rb file, I did set the user's location id to a valid id. Why is current_user.location returning nil? What am I missing in the test? Thank you!
Upvotes: 0
Views: 353
Reputation: 48706
Most probably, it's just that the login process in not correct. This is how i do it. I have a /spec/support/controller_macros.rb and inside :
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = :user
@user = Factory(:user)
sign_in @user
end
end
end
Now, in my controller specs :
describe AbilitiesController do
login_user
describe "POST 'train' ..." do
...
end
...
Upvotes: 2