我在 Rails 2.3.8 项目上使用 Devise 1.0.8。在我的 Controller 测试中,我有:
context "when current_user is admin" do
should "render" do
sign_in Factory(:admin)
get :index
assert_response :success
end
end
在 Controller 本身中,有以下 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 为真。但是,当我不这样做时,current_user 为零,因此它肯定会受到影响。在此先感谢您的帮助。
最佳答案
我用了确认! after_create/build 中资源上的方法,而不是在工厂中设置属性,这就成功了。当网站上有未确认的用户(已注册但尚未点击发送给他们的确认链接的人)时,current_user 等于 true。
关于ruby-on-rails - current_user 使用 Devise 在 Controller 中返回 TrueClass 而不是 User,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4241329/