【发布时间】:2011-12-10 07:25:01
【问题描述】:
在阅读 Mike Hartl 的通过示例学习 Rails 教程时,我收到一个奇怪的 rspec 错误。
419页,代码10.30:代码如下:
it "should have an element for each user" do
get :index
@users[0..2].each do |user|
response.should have_selector("li", :content => user.name)
end
end
当我运行 rspec 时,我收到此错误消息。:
失败:
1) UsersController GET 'index' for signed-in-users should have an element for each user
Failure/Error: @users[0..2].each do |user|
NoMethodError:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]
# ./spec/controllers/users_controller_spec.rb:40
Finished in 2.58 seconds 75 examples, 1 failure
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:38 # UsersController GET 'index' for >signed-in-users should have an element for each user
我做了一些研究,在这个帖子中发现了类似的问题,但我仍然无法通过测试。
有什么想法吗?
参考链接:
Why are my RSpec tests failing, but my app is working?
Cookies do not persist in Rspec on rails 3.1
Rails Tutorial chapter 10.3.3 - 4 RSpec Errors (app working)
【问题讨论】:
-
另外,第一个链接提到在 session_helper.rb 文件中添加了 current_user 和 @current_user。我试过了,但似乎没有纠正任何问题。
-
另一条评论,该应用运行良好。只是这一项测试失败了。很郁闷!
标签: ruby-on-rails rspec