问题描述
我正在学习如何在 Rails 中进行单元测试,但我遇到了一个涉及 Authlogic 的问题.
I'm learning how unit testing is done in Rails, and I've run into a problem involving Authlogic.
根据文档在您的测试中使用 Authlogic 内容所需的几件事:
According to the Documentation there are a few things required to use Authlogic stuff in your tests:
test_helper.rb:
test_helper.rb:
require "authlogic/test_case"
class ActiveSupport::TestCase
setup :activate_authlogic
end
然后在我的功能测试中我可以登录用户:
Then in my functional tests I can login users:
UserSession.create(users(:tester))
问题似乎源于 test_helper.rb 中的 setup :activate_authlogic
行,只要包含该行,运行功能测试时就会出现以下错误:
The problem seems to stem from the setup :activate_authlogic
line in test_helper.rb, whenever that is included, I get the following errors when running functional tests:
NoMethodError: undefined method `request=' for nil:NilClass
authlogic (2.1.3) lib/authlogic/controller_adapters/abstract_adapter.rb:63:in `send'
authlogic (2.1.3) lib/authlogic/controller_adapters/abstract_adapter.rb:63:in `method_missing'
如果我删除 setup :activate_authlogic
并添加 Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
到 test_helper.rb,我的功能测试似乎有效,但现在我的单元测试失败了:
If I remove setup :activate_authlogic
and add instead Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
to test_helper.rb, my functional tests seem to work but now my unit tests fail:
NoMethodError: undefined method `params' for ActiveSupport::TestCase:Class
authlogic (2.1.3) lib/authlogic/controller_adapters/abstract_adapter.rb:30:in `params'
authlogic (2.1.3) lib/authlogic/session/params.rb:96:in `params_credentials'
authlogic (2.1.3) lib/authlogic/session/params.rb:72:in `params_enabled?'
authlogic (2.1.3) lib/authlogic/session/params.rb:66:in `persist_by_params'
authlogic (2.1.3) lib/authlogic/session/callbacks.rb:79:in `persist'
authlogic (2.1.3) lib/authlogic/session/persistence.rb:55:in `persisting?'
authlogic (2.1.3) lib/authlogic/session/persistence.rb:39:in `find'
authlogic (2.1.3) lib/authlogic/acts_as_authentic/session_maintenance.rb:96:in `get_session_information'
authlogic (2.1.3) lib/authlogic/acts_as_authentic/session_maintenance.rb:95:in `each'
authlogic (2.1.3) lib/authlogic/acts_as_authentic/session_maintenance.rb:95:in `get_session_information'
/test/unit/user_test.rb:23:in `test_should_save_user_with_email_password_and_confirmation'
我做错了什么?
推荐答案
将 setup :activate_authlogic 类放在单元测试类中,而不是放在 test_helper 的 ActiveSupport::TestCase 声明中.
Put the setup :activate_authlogic class in your unit test class and not in the ActiveSupport::TestCase declaration in test_helper.
例如
class ExampleControllerTest < ActionController::TestCase
setup :activate_authlogic
end
这篇关于Rails 中的 Authlogic 和单元/功能测试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!