Skip to content

Commit

Permalink
Merge pull request #2461 from rspec/rails-6-1-issue-2451-dev
Browse files Browse the repository at this point in the history
Prevent collisions on let(:name) and let(:method_name)
  • Loading branch information
JonRowe authored and pirj committed Feb 21, 2021
1 parent 44a670d commit 073daad
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Expand Up @@ -12,6 +12,8 @@ Enhancements:
* Improve request spec "controller" scafold when no action is specified.
(Thomas Hareau, #2399)
* Introduce testing snippets concept (Phil Pirozhkov, Benoit Tigeot, #2423)
* Prevent collisions with `let(:name)` for Rails 6.1 and `let(:method_name)` on older
Rails. (Benoit Tigeot, #2461)

Bug Fixes:

Expand Down
14 changes: 7 additions & 7 deletions lib/rspec/rails/fixture_support.rb
Expand Up @@ -9,6 +9,13 @@ module FixtureSupport
include RSpec::Rails::MinitestAssertionAdapter
include ActiveRecord::TestFixtures

# @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?(self)
end

included do
if RSpec.configuration.use_active_record?
include Fixtures
Expand Down Expand Up @@ -50,13 +57,6 @@ def proxy_method_warning_if_called_in_before_context_scope(method_name)
end
end
end

if ::Rails.version.to_f >= 6.1
# @private return the example name for TestFixtures
def name
@example
end
end
end
end
end
Expand Down
57 changes: 57 additions & 0 deletions 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

0 comments on commit 073daad

Please sign in to comment.