Skip to content

Commit 6b4969e

Browse files
authored
Merge pull request #2587 from rspec/shanecav84/include-tagged-logging
Include ActiveSupport::Testing::TaggedLogging for Rails >= 7 (rebased)
2 parents af2b082 + 4a80dda commit 6b4969e

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module RailsExampleGroup
1212
include RSpec::Rails::MinitestLifecycleAdapter
1313
include RSpec::Rails::MinitestAssertionAdapter
1414
include RSpec::Rails::FixtureSupport
15+
include RSpec::Rails::TaggedLoggingAdapter if ::Rails::VERSION::MAJOR >= 7
1516
end
1617
end
1718
end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
if __FILE__ =~ /^snippets/
2+
fail "Snippets are supposed to be run from their own directory to avoid side " \
3+
"effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \
4+
"loaded by the root `.rspec`."
5+
end
6+
7+
# We opt-out from using RubyGems, but `bundler/inline` requires it
8+
require 'rubygems'
9+
10+
require "bundler/inline"
11+
12+
# We pass `false` to `gemfile` to skip the installation of gems,
13+
# because it may install versions that would conflict with versions
14+
# from the main `Gemfile.lock`.
15+
gemfile(false) do
16+
source "https://rubygems.org"
17+
18+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
19+
20+
# Those Gemfiles carefully pick the right versions depending on
21+
# settings in the ENV, `.rails-version` and `maintenance-branch`.
22+
Dir.chdir('..') do
23+
# This Gemfile expects `maintenance-branch` file to be present
24+
# in the current directory.
25+
eval_gemfile 'Gemfile-rspec-dependencies'
26+
# This Gemfile expects `.rails-version` file
27+
eval_gemfile 'Gemfile-rails-dependencies'
28+
end
29+
30+
gem "rspec-rails", path: "../"
31+
end
32+
33+
# Run specs at exit
34+
require "rspec/autorun"
35+
36+
require "rails"
37+
require "active_record/railtie"
38+
require "active_job/railtie"
39+
require "rspec/rails"
40+
41+
ActiveJob::Base.queue_adapter = :test
42+
43+
# This connection will do for database-independent bug reports
44+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
45+
46+
class TestError < StandardError; end
47+
48+
class TestJob < ActiveJob::Base
49+
def perform
50+
raise TestError
51+
end
52+
end
53+
54+
RSpec.describe 'Foo', type: :job do
55+
include ::ActiveJob::TestHelper
56+
57+
describe 'error raised in perform_enqueued_jobs with block' do
58+
it 'raises the explicitly thrown error' do
59+
# Rails 6.1+ wraps unexpected errors in tests
60+
expected_error = if Rails::VERSION::STRING.to_f >= 6.1
61+
Minitest::UnexpectedError.new(TestError)
62+
else
63+
TestError
64+
end
65+
66+
expect { perform_enqueued_jobs { TestJob.perform_later } }
67+
.to raise_error(expected_error)
68+
end
69+
end
70+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module RSpec::Rails
2+
RSpec.describe RailsExampleGroup do
3+
if ::Rails::VERSION::MAJOR >= 7
4+
it 'includes ActiveSupport::Testing::TaggedLogging' do
5+
expect(described_class.include?(::ActiveSupport::Testing::TaggedLogging)).to eq(true)
6+
expect(described_class.private_instance_methods).to include(:tagged_logger)
7+
end
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)