polls_controller.rb
def index
@polls = current_user.polls
end上面对我的用户(current_user) has_many民意测验协会很好。我想我可以在测试中通过设置合适的装置来复制这个结果。
polls_controller_test.rb
setup do
@current_user = users(:bob)
@poll = @current_user.polls.build(title: "Poll")
end
test "should get index" do
@polls = @current_user.polls
get :index
assert_response :success
assert_not_nil assigns(:polls)
endpolls.yml
one:
title: Poll-1
user: bob
two:
title: Poll-2
user: bobusers.yml
bob:
name: Bob Example
email: bob@example.com
password_digest: <%= User.digest('password') %>但是,运行测试将返回
NoMethodError: undefined method `polls' for nil:NilClass我不能这么做吗?(显然不是这样的。)为什么@current_user为零?
发布于 2014-11-18 18:49:24
好吧,想清楚了。与往常一样,这并不是由于某些复杂的错误或语法要求,而是因为我没有在测试设置中指定session:user_id = @current_user.id,这是由@current_user.id助手方法引用的。因此,测试框架将current_user视为零。干杯。
https://stackoverflow.com/questions/26999884
复制相似问题