代码之家  ›  专栏  ›  技术社区  ›  mmacaulay

AuthLogic和Rails中的单元/功能测试有问题

  •  1
  • mmacaulay  · 技术社区  · 14 年前

    我正在学习如何在Rails中完成单元测试,我遇到了一个涉及AuthLogic的问题。

    根据 Documentation 在您的测试中使用AuthLogic材料需要一些东西:

    测试助手.rb:

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

    然后在我的功能测试中,我可以登录用户:

    UserSession.create(users(:tester))
    

    问题似乎源于 setup :activate_authlogic line-in test_helper.rb,只要包含它,在运行功能测试时就会出现以下错误:

    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'
    

    如果我移除 设置:激活authlogic 改为添加 Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self) 为了测试_Helper.rb,我的功能测试似乎可以工作,但是现在我的单元测试失败了:

    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'
    

    我做错什么了?

    4 回复  |  直到 13 年前
        1
  •  8
  •   Rob Di Marco    14 年前

    将设置:activate_authlogic类放在单元测试类中,而不是放在test_helper中的activesupport::testcase声明中。

    例如

    class ExampleControllerTest < ActionController::TestCase
        setup :activate_authlogic
    end
    
        2
  •  3
  •   Jason    14 年前

    我必须包含这样的AuthLogic测试用例模块才能使事情正常工作。

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

    我不知道为什么AuthLogic不包含在我的系统中…但是代码(在authlogic/test_的情况下)在我的系统上不起作用:

    ::Test::Unit::TestCase.send(:include, TestCase) if defined?(::Test::Unit::TestCase)
    
        3
  •  3
  •   Greg    13 年前

    http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/TestCase
    上面的链接很好地描述了所有事情。

        4
  •  2
  •   Ian    14 年前

    Setup:AuthLogic行需要位于ActionController::TestCase类中,而不是ActiveSupport::TestCase。

    在你的测试助手里,把这个放在:

      class ActionController::TestCase
        setup :activate_authlogic
      end