[英]current_user returns TrueClass instead of a User in a controller using Devise
我在Rails 2.3.8项目上使用Devise 1.0.8。 在我的控制器测试中,我有:
context "when current_user is admin" do
should "render" do
sign_in Factory(:admin)
get :index
assert_response :success
end
end
在控制器本身中,有以下before_filter:
def redirect_if_not_admin
puts current_user
redirect_to root_path unless current_user.try(:admin?)
end
这是我测试的输出:
true
false
NoMethodError: undefined method `admin?' for true:TrueClass
因此,当我在测试中使用sign_in时,current_user为true。 但是当我不这样做时,current_user是nil,所以它肯定会受到影响。 在此先感谢您的帮助。
我用过确认! 在after_create / build中使用资源上的方法,而不是在工厂中设置属性就可以了。 当网站上有未经证实的用户时,current_user等于true(已注册但尚未点击发送给他们的确认链接的用户)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.