I am testing my employees_controlle开发者_如何学编程r, 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!
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
...
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论