Skip to content

Commit 4a80dda

Browse files
committed
Define name to work around TaggedLogging
RSpec.current_example.metadata[:name] evaluates to `nil` by the time this is called.
1 parent f54e558 commit 4a80dda

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

lib/rspec/rails/adapters.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,15 @@ def assertion_delegator
181181
#
182182
# @private
183183
TestUnitAssertionAdapter = MinitestAssertionAdapter
184+
185+
# @private
186+
module TaggedLoggingAdapter
187+
require 'active_support/testing/tagged_logging'
188+
include ActiveSupport::Testing::TaggedLogging
189+
190+
# Just a stub as TaggedLogging is calling `name`
191+
def name
192+
end
193+
end
184194
end
185195
end

lib/rspec/rails/example/rails_example_group.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ module RailsExampleGroup
1212
include RSpec::Rails::MinitestLifecycleAdapter
1313
include RSpec::Rails::MinitestAssertionAdapter
1414
include RSpec::Rails::FixtureSupport
15-
16-
if ::Rails::VERSION::MAJOR >= 7
17-
require 'active_support/testing/tagged_logging'
18-
include ActiveSupport::Testing::TaggedLogging
19-
end
15+
include RSpec::Rails::TaggedLoggingAdapter if ::Rails::VERSION::MAJOR >= 7
2016
end
2117
end
2218
end

snippets/include_activesupport_testing_tagged_logger.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# Those Gemfiles carefully pick the right versions depending on
2121
# settings in the ENV, `.rails-version` and `maintenance-branch`.
2222
Dir.chdir('..') do
23-
eval_gemfile 'Gemfile-sqlite-dependencies'
2423
# This Gemfile expects `maintenance-branch` file to be present
2524
# in the current directory.
2625
eval_gemfile 'Gemfile-rspec-dependencies'
@@ -44,19 +43,19 @@
4443
# This connection will do for database-independent bug reports
4544
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
4645

46+
class TestError < StandardError; end
47+
4748
class TestJob < ActiveJob::Base
48-
def perform; end
49+
def perform
50+
raise TestError
51+
end
4952
end
5053

51-
class TestError < StandardError; end
52-
5354
RSpec.describe 'Foo', type: :job do
5455
include ::ActiveJob::TestHelper
5556

5657
describe 'error raised in perform_enqueued_jobs with block' do
5758
it 'raises the explicitly thrown error' do
58-
allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError)
59-
6059
# Rails 6.1+ wraps unexpected errors in tests
6160
expected_error = if Rails::VERSION::STRING.to_f >= 6.1
6261
Minitest::UnexpectedError.new(TestError)

0 commit comments

Comments
 (0)