【发布时间】:2011-05-13 13:59:46
【问题描述】:
我在 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 为真。但是,当我不这样做时, current_user 为零,因此肯定会受到影响。提前感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails unit-testing testing devise