与 Warden 进行功能测试?
我正在尝试进行功能测试,需要使用 Warden 登录。
我已经完成了:
class ActionController::TestCase
include Warden::Test::Helpers
end
我的测试用例很简单:
def test_access_admin_as_superuser
login_as(Person.make(:superuser))
get :index
assert_response :success
assert_nil flash[:warning]
end
我执行了 login_as(user)
并且它似乎可以工作,除非我执行 get '/admin'
时它会失败:
1) Error:
test_access_admin_as_superuser(AdminControllerTest):
NoMethodError: undefined method `user' for nil:NilClass
/home/silas/.rubygems/gems/gems/rails_warden-0.5.2/lib/rails_warden/controller_mixin.rb:21:in `current_user'
app/controllers/application_controller.rb:100:in `require_user'
test/functional/admin_controller_test.rb:20:in `test_access_admin_as_superuser'
有什么想法吗?
I'm trying to do functional testing and need to login with Warden.
I have done:
class ActionController::TestCase
include Warden::Test::Helpers
end
My test case is simple:
def test_access_admin_as_superuser
login_as(Person.make(:superuser))
get :index
assert_response :success
assert_nil flash[:warning]
end
I do a login_as(user)
and it seems to work except when I do a get '/admin'
it will fail with:
1) Error:
test_access_admin_as_superuser(AdminControllerTest):
NoMethodError: undefined method `user' for nil:NilClass
/home/silas/.rubygems/gems/gems/rails_warden-0.5.2/lib/rails_warden/controller_mixin.rb:21:in `current_user'
app/controllers/application_controller.rb:100:in `require_user'
test/functional/admin_controller_test.rb:20:in `test_access_admin_as_superuser'
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Devise 有助手来处理这个问题,如果你只是使用 Warden,这没什么帮助。事情失败的原因是动作控制器在测试环境中以不同的方式设置事情。我整理了一个小宝石,它提供了一个模块,您可以在测试模式下使用 Rails 与 Warden 一起使用: github.com/ajsharp/warden-rspec-rails。
Devise has helpers to deal with this, which is of little help if you're just using Warden. The reason things break is because action controller sets things up in the test environment in a different way. I put together a little gem that provides a module you can include for working with warden with rails in test mode: github.com/ajsharp/warden-rspec-rails.