Rails minitest + authlogic 无法验证

问题描述 投票:0回答:1

我遵循 authlogic 设置,在使用 minitest 进行测试设置期间对管理员用户进行身份验证(https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/test_case.rb)。这看起来很简单,但我在进行测试之前就遇到了“NoMethodError”。

任何人都可以看到我的设置中出现问题的地方吗?

环境

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

宝石档案:

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

失败的测试文件示例:

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'
ruby-on-rails minitest authlogic
1个回答
0
投票

您只需将

Authlogic::TestCase
模块包含在
ActiveSupport::TestCase
类中即可。

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
© www.soinside.com 2019 - 2024. All rights reserved.