rspec-rails: undefined local variable or method `tagged_logger' during perform_enqueued_jobs

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-darwin21] Rails version: 7.0.0 RSpec 3.10

  • rspec-core 3.10.1
  • rspec-expectations 3.10.1
  • rspec-mocks 3.10.2
  • rspec-rails 5.0.2
  • rspec-support 3.10.3

Observed behaviour

Inside an rspec support module, my app is calling perform_enqueued_jobs. The spec in question that is using this support module is expecting this call to raise an exception.

When I attempt to run this spec, the following is raised:

got #<NameError: undefined local variable or method `tagged_logger'

When I debug into perform_enqueued_jobs, I am taken into _assert_nothing_raised_or_warn inside activesupport-7.0.0/lib/active_support/testing/assertions.rb, When debugging the block below, it shows e as having the expected exception.

        rescue Minitest::UnexpectedError => e
          if tagged_logger && tagged_logger.warn?
            warning = <<~MSG
              #{self.class} - #{name}: #{e.error.class} raised.
              If you expected this exception, use `assert_raises` as near to the code that raises as possible.
              Other block based assertions (e.g. `#{assertion}`) can be used, as long as `assert_raises` is inside their block.
            MSG
            tagged_logger.warn warning
          end

          raise
        end

Debugging further reveals that the if tagged_logger check above is causing the undefined error.

I reported this on Rails forum here and a member of the core team suggested the following:

“This seems to be a bug in RSpec that doesn’t implement the tagged_logger. Please report the issue in the RSpec issue tracker.”

Expected behaviour

The spec should finish and allow to raise_error to check for the expected exception. This spec is working as expected in Rails 6.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 13
  • Comments: 25 (12 by maintainers)

Commits related to this issue

Most upvoted comments

I’m using the following crappy patch, as including the module into RSpec::Rails::RailsExampleGroup did not do anything.

Also defined a dummy name method (sigh), to resolve the subsequent issue that @jkwuc89 raises just above (which this also runs into)

if Rails::VERSION::MAJOR >= 7
  require 'rspec/rails/version'

  RSpec::Core::ExampleGroup.module_eval do
    include ActiveSupport::Testing::TaggedLogging

    def name
      'foobar'
    end
  end
end

PRs would be accepted to solve this, RailsExampleGroup should conditionally (as in, if on Rails 7) include this module, and ideally we’d want a snippet testing it.

I’m still seeing this issue with Ruby (3.3.5), Rails (7.1.4), rspec (3.13.0), rspec-rails (7.0.1). To me it’s pointing here:

NameError:
       undefined local variable or method `tagged_logger' for an instance of FactoryBot::SyntaxRunner

But don’t think is has to do with FactoryBot, it’s seems is just because I have my perform_enqueued_jobs in a factory.

Any news on this cc @jkwuc89 @pirj @JonRowe

@JonRowe the problem still persists, version 6.0.1

Here is a quick monkey patch using @pirj suggested solution

# spec/rspec_helper.rb

if Rails::VERSION::MAJOR >= 7
  require 'rspec/rails/version'
  
  if Gem::Version.create(RSpec::Rails::Version::STRING) < Gem::Version.create("5.1.0")
    RSpec::Rails::RailsExampleGroup.module_eval do
      include ActiveSupport::Testing::TaggedLogging
    end
  end
end

@pirj I just saw this error in 6.0.1, and the monkey patch above https://github.com/rspec/rspec-rails/issues/2545#issuecomment-1029539971 can fix the issue.

However, I find I missed type: :job in my spec. If I add it, everything works fine. I think it’s because the error is raised by RSpec::Core::ExampleGroup if I don’t add type: :job, so the monkey patch works. When running a spec with type: :job, RSpec::Rails::RailsExampleGroup is included, so the fix works. Does it make sense?

It feels like they are checking for a tagged logger by assuming it exists, they could easily fix this by checking its defined

I don’t know, that looks to me like the factory needs to have the tagger logger included and thats out of scope for us.

@sebaherrera07 Can you please:

  • open a new issue
  • link to this one
  • provide a backtrace
  • provide a repro example

I’m running this code:

describe “#request_token” do it “returns a successful response” do post :request_token, params: { username: ‘XXX’, password: ‘XXX’ } expect(response).to have_http_status(:success) end end

and am getting this error:

=> There was an error building fixtures => #<ArgumentError: wrong number of arguments (given 0, expected 1+)>

/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/fixture_builder-7a042cc63699/lib/fixture_builder/namer.rb:16:in name' /.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/fixture_builder-7a042cc63699/lib/fixture_builder/delegations.rb:16:in name’ /.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.7.2/lib/active_support/testing/assertions.rb:254:in rescue in _assert_nothing_raised_or_warn' /.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/activesupport-7.0.7.2/lib/active_support/testing/assertions.rb:249:in _assert_nothing_raised_or_warn’

I got past the first “tagged logger” error by adding include ActiveSupport::Testing::TaggedLogging, however I’m seeing this afterward and haven’t seen anything addressing it. Adding def name did not work for me.

Because this is only for Rails example groups. But the patch you refer to does patch it for all example groups. Do you add proper metadata to your Raisl-related example groups?

I also see the same error with 6.0.1, my code looks similar

      specify do
        expect { perform_enqueued_jobs { dispatch } }
          .to not_change(ActionMailer::Base.deliveries, :size)
          .and not_raise_error

Ruby 3.2.1, rails 7.0.4

I think rspec-rails should be responsible for making sure the features of ActiveSupport::TestCase it uses works with RSpec, nor Rails that should check if its own API is existent.

I’ll take a deeper dive into this and report what I find.