开发者

Problem with Authlogic and Unit/Functional Tests in Rails

开发者 https://www.devze.com 2022-12-27 07:20 出处:网络
I\'m learning how unit testing is done in Rails, and I\'ve run into a problem involving Au开发者_JS百科thlogic.

I'm learning how unit testing is done in Rails, and I've run into a problem involving Au开发者_JS百科thlogic.

According to the Documentation there are a few things required to use Authlogic stuff in your tests:

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))

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'

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'

What am I doing wrong?


Put the setup :activate_authlogic class in your unit test class and not in the ActiveSupport::TestCase declaration in test_helper.

e.g.

class ExampleControllerTest < ActionController::TestCase
    setup :activate_authlogic
end


I had to include the Authlogic test case module like this in order to get things working.

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

I'm not sure why Authlogic wouldn't include itself on my system... but the code (in authlogic/test_case) doesn't work on my system:

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


http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/TestCase
evertything is very well described on the link above.


The setup :authlogic line needs to be in the ActionController::TestCase class, not the ActiveSupport::TestCase.

Inside your test_helper, put this in:

  class ActionController::TestCase
    setup :activate_authlogic
  end
0

精彩评论

暂无评论...
验证码 换一张
取 消