问题描述
我有一个资源,其中的新操作需要用户登录才能查看.如果用户在未登录的情况下尝试创建新资源,他们将被重定向(302'd)到登录页面.我的功能测试如下所示:
I have a resource where the new action requires a user to be logged in to view. If a user tries to create a new resource without being logged in, they are redirected (302'd) to the login page. My functional test looks like this:
test "should not get new unless logged in" do
get :new
assert_response :redirect
end
堆栈跟踪看起来像这样:
The stacktrace looks something like this:
ArgumentError: uncaught throw :warden
/.../gems/warden-1.1.1/lib/warden/proxy.rb:114:in `throw'
/.../gems/ruby-1.9.2-p318/gems/warden-1.1.1/lib/warden/proxy.rb:114:in `authenticate!'
/.../gems/ruby-1.9.2-p318/gems/devise-2.0.4/lib/devise/controllers/helpers.rb:48:in `authenticate_user!'
在新操作之前,我有一个 before_filter 来验证用户.
I have a before_filter to authenticate_user before the new action.
我明白为什么authenticate_user!失败了,但我不明白为什么它会抛出错误.它不应该只是像在 webapp 中那样表现,即.将用户重定向到登录页面?
I understand why authenticate_user! is failing but I can't understand why its throwing an error. Shouldn't it just behave as it does in the webapp ie. redirects user to the log in page?
谢谢.
推荐答案
Do as 文档 说:
class ActionController::TestCase
include Devise::TestHelpers
end
特别是不要把include Devise::TestHelpers代码>
到class
ActiveSupport::TestCase
.
Particularly, don't put include Devise::TestHelpers
into class
ActiveSupport::TestCase
.
这篇关于在功能性 Rails 测试中为 authentication_user 抛出设计管理员错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!