From 8edb6608d474b1559452df8ff602b4ed8711cff0 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Wed, 17 Feb 2021 23:22:11 +0100 Subject: [PATCH 1/3] Add snippets for method collision on :name and :method_name --- snippets/avoid_fixture_name_collision.rb | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 snippets/avoid_fixture_name_collision.rb diff --git a/snippets/avoid_fixture_name_collision.rb b/snippets/avoid_fixture_name_collision.rb new file mode 100644 index 0000000000..8f1768acc8 --- /dev/null +++ b/snippets/avoid_fixture_name_collision.rb @@ -0,0 +1,57 @@ +if __FILE__ =~ /^snippets/ + fail "Snippets are supposed to be run from their own directory to avoid side " \ + "effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \ + "loaded by the root `.rspec`." +end + +# We opt-out from using RubyGems, but `bundler/inline` requires it +require 'rubygems' + +require "bundler/inline" + +# We pass `false` to `gemfile` to skip the installation of gems, +# because it may install versions that would conflict with versions +# from the main `Gemfile.lock`. +gemfile(false) do + source "https://rubygems.org" + + git_source(:github) { |repo| "https://github.com/#{repo}.git" } + + # Those Gemfiles carefully pick the right versions depending on + # settings in the ENV, `.rails-version` and `maintenance-branch`. + Dir.chdir('..') do + eval_gemfile 'Gemfile-sqlite-dependencies' + # This Gemfile expects `maintenance-branch` file to be present + # in the current directory. + eval_gemfile 'Gemfile-rspec-dependencies' + # This Gemfile expects `.rails-version` file + eval_gemfile 'Gemfile-rails-dependencies' + end + + gem "rspec-rails", path: "../" +end + +# Run specs at exit +require "rspec/autorun" + +require "rails" +require "active_record/railtie" +require "rspec/rails" + +# This connection will do for database-independent bug reports +ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") + +RSpec.configure do |config| + config.use_transactional_fixtures = true +end + +RSpec.describe 'Foo' do + subject { true } + + # Rails 6.1 and after + let(:name) { raise "Should never raise" } + # Before Rails 6.1 + let(:method_name) { raise "Should never raise" } + + it { is_expected.to be_truthy } +end From 4aa5e0b4ad70dda0e243f15ac78d729b2a6694fa Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Wed, 17 Feb 2021 23:30:09 +0100 Subject: [PATCH 2/3] Prevent collisions on let :name and :method_name in rspec-rails examples Fix: https://github.com/rspec/rspec-rails/issues/2451 --- lib/rspec/rails/fixture_support.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 02786d3c29..1a7221e829 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -9,11 +9,11 @@ module FixtureSupport include RSpec::Rails::MinitestAssertionAdapter include ActiveRecord::TestFixtures - if ::Rails.version.to_f >= 6.1 - # @private return the example name for TestFixtures - def name - @example - end + # @private prevent ActiveSupport::TestFixtures to start a DB transaction. + # 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?(@example) end included do From ee760f2611e5c8f44494381ebe8546cceb09fcde Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Thu, 18 Feb 2021 21:52:40 +0100 Subject: [PATCH 3/3] @example resolve to nil, self: RSpec::ExampleGroups::ObservesACallToName --- lib/rspec/rails/fixture_support.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 1a7221e829..12acfdfbd6 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -13,7 +13,7 @@ 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?(@example) + use_transactional_tests && !self.class.uses_transaction?(self) end included do