Skip to content

Commit 9ce49af

Browse files
st0012pirj
authored andcommitted
Fix FixtureSupport's run_in_transaction? method
`ActiveRecord::TestFixture`'s `uses_transaction` is designed to be used like this: ```ruby uses_transaction :the_test_method_name ``` And in RSpec, the method name would be the example's name. ```ruby uses_transaction "does someting" it "does someting" {} ``` But in the current implementation, it's passing the example object instead of its name, which would always fail the name comparison in https://github.com/rails/rails/blob/adc0146a07ccc72405aec78ccb65aac3502a4300/activerecord/lib/active_record/test_fixtures.rb#L94-L97 So this commit fixes the issue by passing the example's name instead of the example object.
1 parent efdc7b5 commit 9ce49af

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Enhancements:
55

66
* Make the API request scaffold template more consistent and compatible with
77
Rails 6.1. (Naoto Hamada, #2484)
8+
* Provide support to turn off transactional tests for select examples.
9+
(Stan Lo, #2495)
810

911
### 5.0.1 / 2021-03-18
1012
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v5.0.0...v5.0.1)

lib/rspec/rails/fixture_support.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module FixtureSupport
1313
# Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after
1414
# and let(:method_name) before Rails 6.1.
1515
def run_in_transaction?
16-
use_transactional_tests && !self.class.uses_transaction?(self)
16+
current_example_name = (RSpec.current_example && RSpec.current_example.metadata[:description])
17+
use_transactional_tests && !self.class.uses_transaction?(current_example_name)
1718
end
1819

1920
included do

spec/rspec/rails/fixture_support_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ module RSpec::Rails
1212
end
1313
end
1414

15+
context "with use_transactional_tests set to true" do
16+
it "works with #uses_transaction helper" do
17+
group = RSpec::Core::ExampleGroup.describe do
18+
include FixtureSupport
19+
self.use_transactional_tests = true
20+
21+
uses_transaction "doesn't run in transaction"
22+
23+
it "doesn't run in transaction" do
24+
expect(ActiveRecord::Base.connection.transaction_open?).to eq(false)
25+
end
26+
27+
it "runs in transaction" do
28+
expect(ActiveRecord::Base.connection.transaction_open?).to eq(true)
29+
end
30+
end
31+
32+
expect_to_pass(group)
33+
end
34+
35+
def expect_to_pass(group)
36+
result = group.run(failure_reporter)
37+
failure_reporter.exceptions.map { |e| raise e }
38+
expect(result).to be true
39+
end
40+
end
41+
1542
it "will allow #setup_fixture to run successfully", skip: Rails.version.to_f <= 6.0 do
1643
group = RSpec::Core::ExampleGroup.describe do
1744
include FixtureSupport

0 commit comments

Comments
 (0)