diff --git a/lib/rspec/rails/example/rails_example_group.rb b/lib/rspec/rails/example/rails_example_group.rb index 0643b9ae1..ed343de35 100644 --- a/lib/rspec/rails/example/rails_example_group.rb +++ b/lib/rspec/rails/example/rails_example_group.rb @@ -3,6 +3,7 @@ require 'rspec/rails/matchers' if ::Rails::VERSION::MAJOR >= 7 + require 'active_support/current_attributes/test_helper' require 'active_support/execution_context/test_helper' end @@ -18,6 +19,7 @@ module RailsExampleGroup include RSpec::Rails::FixtureSupport if ::Rails::VERSION::MAJOR >= 7 include RSpec::Rails::TaggedLoggingAdapter + include ActiveSupport::CurrentAttributes::TestHelper include ActiveSupport::ExecutionContext::TestHelper end end diff --git a/spec/rspec/rails/example/rails_example_group_spec.rb b/spec/rspec/rails/example/rails_example_group_spec.rb index 7faac1646..7dbe13788 100644 --- a/spec/rspec/rails/example/rails_example_group_spec.rb +++ b/spec/rspec/rails/example/rails_example_group_spec.rb @@ -1,9 +1,7 @@ module RSpec::Rails RSpec.describe RailsExampleGroup do - if ::Rails::VERSION::MAJOR >= 7 - it 'supports tagged_logger' do - expect(described_class.private_instance_methods).to include(:tagged_logger) - end + it 'supports tagged_logger', if: ::Rails::VERSION::MAJOR >= 7 do + expect(described_class.private_instance_methods).to include(:tagged_logger) end it 'does not leak context between example groups', if: ::Rails::VERSION::MAJOR >= 7 do @@ -32,5 +30,31 @@ module RSpec::Rails expect(results).to all be true end + + it 'will not leak ActiveSupport::CurrentAttributes between examples', if: ::Rails::VERSION::MAJOR >= 7 do + group = + RSpec::Core::ExampleGroup.describe("A group", order: :defined) do + include RSpec::Rails::RailsExampleGroup + + # rubocop:disable Lint/ConstantDefinitionInBlock + class CurrentSample < ActiveSupport::CurrentAttributes + attribute :request_id + end + # rubocop:enable Lint/ConstantDefinitionInBlock + + it 'sets a current attribute' do + CurrentSample.request_id = '123' + expect(CurrentSample.request_id).to eq('123') + end + + it 'does not leak current attributes' do + expect(CurrentSample.request_id).to eq(nil) + end + end + + expect( + group.run(failure_reporter) ? true : failure_reporter.exceptions + ).to be true + end end end