Skip to content

Commit

Permalink
Define name to work around TaggedLogging
Browse files Browse the repository at this point in the history
RSpec.current_example.metadata[:name] evaluates to `nil` by the time
this is called.
  • Loading branch information
pirj committed Jul 17, 2022
1 parent d372749 commit 0096e1b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
10 changes: 10 additions & 0 deletions lib/rspec/rails/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,15 @@ def assertion_delegator
#
# @private
TestUnitAssertionAdapter = MinitestAssertionAdapter

# @private
module TaggedLoggingAdapter
require 'active_support/testing/tagged_logging'
include ActiveSupport::Testing::TaggedLogging

# Just a stub as TaggedLogging is calling `name`
def name
end
end
end
end
6 changes: 1 addition & 5 deletions lib/rspec/rails/example/rails_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ module RailsExampleGroup
include RSpec::Rails::MinitestLifecycleAdapter
include RSpec::Rails::MinitestAssertionAdapter
include RSpec::Rails::FixtureSupport

if ::Rails::VERSION::MAJOR >= 7
require 'active_support/testing/tagged_logging'
include ActiveSupport::Testing::TaggedLogging
end
include RSpec::Rails::TaggedLoggingAdapter if ::Rails::VERSION::MAJOR >= 7
end
end
end
11 changes: 6 additions & 5 deletions snippets/include_activesupport_testing_tagged_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
end

gem "rspec-rails", path: "../"
gem 'pry-byebug'
end

# Run specs at exit
Expand All @@ -44,19 +45,19 @@
# This connection will do for database-independent bug reports
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

class TestError < StandardError; end

class TestJob < ActiveJob::Base
def perform; end
def perform
raise TestError
end
end

class TestError < StandardError; end

RSpec.describe 'Foo', type: :job do
include ::ActiveJob::TestHelper

describe 'error raised in perform_enqueued_jobs with block' do
it 'raises the explicitly thrown error' do
allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError)

# Rails 6.1+ wraps unexpected errors in tests
expected_error = if Rails::VERSION::STRING.to_f >= 6.1
Minitest::UnexpectedError.new(TestError)
Expand Down

0 comments on commit 0096e1b

Please sign in to comment.