From 9ce49af26a8aaafbcc6e626a6e2f8bc095d7d740 Mon Sep 17 00:00:00 2001 From: st0012 Date: Mon, 19 Apr 2021 19:37:47 +0800 Subject: [PATCH] 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. --- Changelog.md | 2 ++ lib/rspec/rails/fixture_support.rb | 3 ++- spec/rspec/rails/fixture_support_spec.rb | 27 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 1e257c2014..a8a955ce83 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,8 @@ Enhancements: * Make the API request scaffold template more consistent and compatible with Rails 6.1. (Naoto Hamada, #2484) +* Provide support to turn off transactional tests for select examples. + (Stan Lo, #2495) ### 5.0.1 / 2021-03-18 [Full Changelog](https://github.com/rspec/rspec-rails/compare/v5.0.0...v5.0.1) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 63334b793e..9c7d1eeb58 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -13,7 +13,8 @@ module FixtureSupport # Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after # and let(:method_name) before Rails 6.1. def run_in_transaction? - use_transactional_tests && !self.class.uses_transaction?(self) + current_example_name = (RSpec.current_example && RSpec.current_example.metadata[:description]) + use_transactional_tests && !self.class.uses_transaction?(current_example_name) end included do diff --git a/spec/rspec/rails/fixture_support_spec.rb b/spec/rspec/rails/fixture_support_spec.rb index 674a065fab..62b08a303f 100644 --- a/spec/rspec/rails/fixture_support_spec.rb +++ b/spec/rspec/rails/fixture_support_spec.rb @@ -12,6 +12,33 @@ module RSpec::Rails end end + context "with use_transactional_tests set to true" do + it "works with #uses_transaction helper" do + group = RSpec::Core::ExampleGroup.describe do + include FixtureSupport + self.use_transactional_tests = true + + uses_transaction "doesn't run in transaction" + + it "doesn't run in transaction" do + expect(ActiveRecord::Base.connection.transaction_open?).to eq(false) + end + + it "runs in transaction" do + expect(ActiveRecord::Base.connection.transaction_open?).to eq(true) + end + end + + expect_to_pass(group) + end + + def expect_to_pass(group) + result = group.run(failure_reporter) + failure_reporter.exceptions.map { |e| raise e } + expect(result).to be true + end + end + it "will allow #setup_fixture to run successfully", skip: Rails.version.to_f <= 6.0 do group = RSpec::Core::ExampleGroup.describe do include FixtureSupport