Skip to content

Commit

Permalink
Merge pull request #2752 from javierjulio/patch-1
Browse files Browse the repository at this point in the history
Reset CurrentAttributes on each rails example
  • Loading branch information
JonRowe authored Apr 10, 2024
2 parents 2787525 + 3013459 commit 24ca7fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/rspec/rails/example/rails_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
32 changes: 28 additions & 4 deletions spec/rspec/rails/example/rails_example_group_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 24ca7fc

Please sign in to comment.