Rails中的Authlogic和单元/function测试问题

我正在学习如何在Rails中完成unit testing,并且我遇到了涉及Authlogic的问题。

根据文档 ,在测试中使用Authlogic的东西需要一些东西:

test_helper.rb中:

require "authlogic/test_case" class ActiveSupport::TestCase setup :activate_authlogic end 

然后在我的function测试中我可以登录用户:

 UserSession.create(users(:tester)) 

问题似乎源于test_helper.rb中的setup :activate_authlogic行,只要包含它,运行function测试时会出现以下错误:

 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,我的function测试似乎工作但现在我的unit testing失败:

 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类放在unit testing类中,而不是放在test_helper中的ActiveSupport :: TestCase声明中。

例如

 class ExampleControllerTest < ActionController::TestCase setup :activate_authlogic end 

我必须包含这样的Authlogic测试用例模块才能使工作正常进行。

 class ExampleControllerTest < ActionController::TestCase include Authlogic::TestCase setup :activate_authlogic end 

我不确定为什么Authlogic不会将自己包含在我的系统中...但是代码(在authlogic / test_case中)在我的系统上不起作用:

 ::Test::Unit::TestCase.send(:include, TestCase) if defined?(::Test::Unit::TestCase) 

http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/TestCase
evertything在上面的链接上有很好的描述。

setup:authlogic行需要在ActionController :: TestCase类中,而不是ActiveSupport :: TestCase。

在test_helper中,将其放入:

  class ActionController::TestCase setup :activate_authlogic end