Skip to content

Commit 9272757

Browse files
authored
Merge pull request #2461 from rspec/rails-6-1-issue-2451-dev
Prevent collisions on let(:name) and let(:method_name)
2 parents 9883300 + ee760f2 commit 9272757

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

lib/rspec/rails/fixture_support.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ module FixtureSupport
99
include RSpec::Rails::MinitestAssertionAdapter
1010
include ActiveRecord::TestFixtures
1111

12-
if ::Rails.version.to_f >= 6.1
13-
# @private return the example name for TestFixtures
14-
def name
15-
@example
16-
end
12+
# @private prevent ActiveSupport::TestFixtures to start a DB transaction.
13+
# Monkey patched to avoid collisions with 'let(:name)' in Rails 6.1 and after
14+
# and let(:method_name) before Rails 6.1.
15+
def run_in_transaction?
16+
use_transactional_tests && !self.class.uses_transaction?(self)
1717
end
1818

1919
included do
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
if __FILE__ =~ /^snippets/
2+
fail "Snippets are supposed to be run from their own directory to avoid side " \
3+
"effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \
4+
"loaded by the root `.rspec`."
5+
end
6+
7+
# We opt-out from using RubyGems, but `bundler/inline` requires it
8+
require 'rubygems'
9+
10+
require "bundler/inline"
11+
12+
# We pass `false` to `gemfile` to skip the installation of gems,
13+
# because it may install versions that would conflict with versions
14+
# from the main `Gemfile.lock`.
15+
gemfile(false) do
16+
source "https://rubygems.org"
17+
18+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
19+
20+
# Those Gemfiles carefully pick the right versions depending on
21+
# settings in the ENV, `.rails-version` and `maintenance-branch`.
22+
Dir.chdir('..') do
23+
eval_gemfile 'Gemfile-sqlite-dependencies'
24+
# This Gemfile expects `maintenance-branch` file to be present
25+
# in the current directory.
26+
eval_gemfile 'Gemfile-rspec-dependencies'
27+
# This Gemfile expects `.rails-version` file
28+
eval_gemfile 'Gemfile-rails-dependencies'
29+
end
30+
31+
gem "rspec-rails", path: "../"
32+
end
33+
34+
# Run specs at exit
35+
require "rspec/autorun"
36+
37+
require "rails"
38+
require "active_record/railtie"
39+
require "rspec/rails"
40+
41+
# This connection will do for database-independent bug reports
42+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
43+
44+
RSpec.configure do |config|
45+
config.use_transactional_fixtures = true
46+
end
47+
48+
RSpec.describe 'Foo' do
49+
subject { true }
50+
51+
# Rails 6.1 and after
52+
let(:name) { raise "Should never raise" }
53+
# Before Rails 6.1
54+
let(:method_name) { raise "Should never raise" }
55+
56+
it { is_expected.to be_truthy }
57+
end

0 commit comments

Comments
 (0)