Skip to content

Commit f54e558

Browse files
shanecav84pirj
authored andcommitted
Include ActiveSupport::Testing::TaggedLogging for Rails >= 7
1 parent 8f449ab commit f54e558

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

lib/rspec/rails/example/rails_example_group.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ 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
1520
end
1621
end
1722
end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
eval_gemfile 'Gemfile-sqlite-dependencies'
24+
# This Gemfile expects `maintenance-branch` file to be present
25+
# in the current directory.
26+
eval_gemfile 'Gemfile-rspec-dependencies'
27+
# This Gemfile expects `.rails-version` file
28+
eval_gemfile 'Gemfile-rails-dependencies'
29+
end
30+
31+
gem "rspec-rails", path: "../"
32+
end
33+
34+
# Run specs at exit
35+
require "rspec/autorun"
36+
37+
require "rails"
38+
require "active_record/railtie"
39+
require "active_job/railtie"
40+
require "rspec/rails"
41+
42+
ActiveJob::Base.queue_adapter = :test
43+
44+
# This connection will do for database-independent bug reports
45+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
46+
47+
class TestJob < ActiveJob::Base
48+
def perform; end
49+
end
50+
51+
class TestError < StandardError; end
52+
53+
RSpec.describe 'Foo', type: :job do
54+
include ::ActiveJob::TestHelper
55+
56+
describe 'error raised in perform_enqueued_jobs with block' do
57+
it 'raises the explicitly thrown error' do
58+
allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError)
59+
60+
# Rails 6.1+ wraps unexpected errors in tests
61+
expected_error = if Rails::VERSION::STRING.to_f >= 6.1
62+
Minitest::UnexpectedError.new(TestError)
63+
else
64+
TestError
65+
end
66+
67+
expect { perform_enqueued_jobs { TestJob.perform_later } }
68+
.to raise_error(expected_error)
69+
end
70+
end
71+
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)