Search code examples
ruby-on-railsminitestauthlogic

Rails minitest + authlogic cannot authenticate


I am following the authlogic setup for authenticating an admin user during testing setup using minitest ( https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/test_case.rb ). It seems very straightforward, yet I am encountering a "NoMethodError" before even hitting my tests.

Can anyone see where the issue is arising in my setup?

Environment

>> ruby -v
ruby 3.3.1 (2024-04-23 revision c56cd86388) [arm64-darwin23]
>> rails -v
Rails 7.0.8.4

Gemfile:

...
gem 'authlogic'
...
group :test do
  gem 'faker'
  gem 'minitest-rails'
end
...

test_helper.rb:

# frozen_string_literal: true

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'minitest/rails'
require 'authlogic/test_case'

module ActiveSupport
  class TestCase
    # Run tests in parallel with specified workers
    parallelize(workers: :number_of_processors)

    # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
    # fixtures :all

    # Add more helper methods to be used by all tests here...
    include FactoryBot::Syntax::Methods
  end
end

Example failing test file:

class DevelopmentGroupsControllerTest < ActionDispatch::IntegrationTest
  setup :activate_authlogic

  test 'should get index' do
    get development_groups_url
    assert_response :success
  end
...

Minitest::UnexpectedError: NoMethodError: undefined method `activate_authlogic' for an instance of DevelopmentGroupsControllerTest
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/actionpack-7.0.8.2/lib/action_dispatch/testing/assertions/routing.rb:187:in `method_missing'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/actionpack-7.0.8.2/lib/action_dispatch/testing/integration.rb:431:in `method_missing'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:400:in `block in make_lambda'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:199:in `block (2 levels) in halting'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:687:in `block (2 levels) in default_terminator'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:686:in `catch'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:686:in `block in default_terminator'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:200:in `block in halting'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:595:in `block in invoke_before'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:595:in `each'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:595:in `invoke_before'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:106:in `run_callbacks'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/testing/setup_and_teardown.rb:41:in `before_setup'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/actionpack-7.0.8.2/lib/action_dispatch/testing/integration.rb:331:in `before_setup'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activejob-7.0.8.2/lib/active_job/test_helper.rb:48:in `before_setup'
    /Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/railties-7.0.8.2/lib/rails/test_help.rb:52:in `before_setup'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/actionpack-7.0.8.2/lib/action_dispatch/testing/assertions/routing.rb:187:in `method_missing'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/actionpack-7.0.8.2/lib/action_dispatch/testing/integration.rb:431:in `method_missing'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:400:in `block in make_lambda'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:199:in `block (2 levels) in halting'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:687:in `block (2 levels) in default_terminator'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:686:in `catch'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:686:in `block in default_terminator'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:200:in `block in halting'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:595:in `block in invoke_before'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:595:in `each'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:595:in `invoke_before'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/callbacks.rb:106:in `run_callbacks'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activesupport-7.0.8.2/lib/active_support/testing/setup_and_teardown.rb:41:in `before_setup'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/actionpack-7.0.8.2/lib/action_dispatch/testing/integration.rb:331:in `before_setup'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/activejob-7.0.8.2/lib/active_job/test_helper.rb:48:in `before_setup'
/Users/jmiller/.rbenv/versions/3.3.1/lib/ruby/gems/3.3.0/gems/railties-7.0.8.2/lib/rails/test_help.rb:52:in `before_setup'

Solution

  • You just need to include the Authlogic::TestCase module within the ActiveSupport::TestCase class.

    module ActiveSupport
      class TestCase
        # Run tests in parallel with specified workers
        parallelize(workers: :number_of_processors)
    
        # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
        # fixtures :all
    
        # Add more helper methods to be used by all tests here...
        include FactoryBot::Syntax::Methods
        include Authlogic::TestCase
      end
    end