Rspec 错误 - 当前用户与另一个模型之间的关系
我正在测试我的员工控制器,在登录用户的索引测试中,我收到一个似乎与我的模型关系相关的错误。
测试是:
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
我设置了 render_views 。我还将每个用户绑定到一个位置,并且员工索引视图应该显示用户位置的员工。在我的索引视图中,我有以下代码来显示员工位置的名称:
<h1>Listing employees for <%= current_user.location.name %> </h1>
当我运行测试时,我收到一条错误消息,其中包含 “ActionView::模板::错误: undefined method 'name for nil:NilClass"
在我的factories.rb文件中,我确实将用户的位置id设置为有效id。为什么current_user.location返回nil?我在测试中缺少什么?谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最有可能的是,这只是登录过程不正确。我就是这样做的。我有一个 /spec/support/controller_macros.rb 和里面:
现在,在我的控制器规格中:
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 :
Now, in my controller specs :