1

I'm learning how unit testing is done in Rails, and I've run into a problem involving Authlogic.

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?

4 Answers 4

8

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
Sign up to request clarification or add additional context in comments.

Comments

3

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)

1 Comment

I also had to require 'authlogic/test_case', but there may be something wonky with my setup.
3

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.