From 0507fc282f8c64b90aa183d38c302e997ae23398 Mon Sep 17 00:00:00 2001 From: Edouard CHIN Date: Mon, 2 Dec 2019 15:30:25 +0100 Subject: [PATCH] Fix `WrongScopeError` being thrown on Rails master: - ### Problem Rails made a change in rails/rails@d4367eb72601f6e5bba3206443e9d141e9753af8 and TestFixtures don't make use of `method_name` anymore, but simply `name`. `name` on RSpec raises a `WrongScopeError` when called within an example. This patch overrides `name` to return the example name instead. --- lib/rspec/rails/fixture_support.rb | 6 ++++++ spec/rspec/rails/configuration_spec.rb | 8 ++++++-- spec/rspec/rails/fixture_support_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/rspec/rails/fixture_support.rb b/lib/rspec/rails/fixture_support.rb index 1855588261..1feddbba85 100644 --- a/lib/rspec/rails/fixture_support.rb +++ b/lib/rspec/rails/fixture_support.rb @@ -50,6 +50,12 @@ def self.proxy_method_warning_if_called_in_before_context_scope(method_name) end end + if ::Rails.version.to_f >= 6.1 + def name + @example + end + end + fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures end end diff --git a/spec/rspec/rails/configuration_spec.rb b/spec/rspec/rails/configuration_spec.rb index 3b904cd97b..96dc0b99d4 100644 --- a/spec/rspec/rails/configuration_spec.rb +++ b/spec/rspec/rails/configuration_spec.rb @@ -196,7 +196,9 @@ def in_inferring_type_from_location_environment it "metadata `type: :request` sets up request example groups" do a_rails_app = double("Rails application") - the_rails_module = Module.new + the_rails_module = Module.new { + def self.version; end; + } allow(the_rails_module).to receive(:application) { a_rails_app } version = ::Rails::VERSION stub_const "Rails", the_rails_module @@ -230,7 +232,9 @@ def in_inferring_type_from_location_environment it "metadata `type: :feature` sets up feature example groups" do a_rails_app = double("Rails application") - the_rails_module = Module.new + the_rails_module = Module.new { + def self.version; end; + } allow(the_rails_module).to receive(:application) { a_rails_app } version = ::Rails::VERSION stub_const "Rails", the_rails_module diff --git a/spec/rspec/rails/fixture_support_spec.rb b/spec/rspec/rails/fixture_support_spec.rb index 29243db089..812eff99c7 100644 --- a/spec/rspec/rails/fixture_support_spec.rb +++ b/spec/rspec/rails/fixture_support_spec.rb @@ -13,5 +13,27 @@ module RSpec::Rails expect(group).to respond_to(:fixture_path=) end end + + it "doesn't raise a WrongScopeError" do + skip if Rails.version.to_f < 6.1 + + group = RSpec::Core::ExampleGroup.describe do + include FixtureSupport + end + + expect { group.new.name }.to_not raise_error + end + + it "setup_fixture successfuly" do + skip if Rails.version.to_f < 6.1 + + group = RSpec::Core::ExampleGroup.describe do + include FixtureSupport + + self.use_transactional_tests = false + end + + expect { group.new.setup_fixtures }.to_not raise_error + end end end