diff --git a/spec/rspec/rails/example/controller_example_group_spec.rb b/spec/rspec/rails/example/controller_example_group_spec.rb index ee1a701995..b801f58df9 100644 --- a/spec/rspec/rails/example/controller_example_group_spec.rb +++ b/spec/rspec/rails/example/controller_example_group_spec.rb @@ -31,18 +31,16 @@ def group_for(klass) # before #738 implicit subject definition for controllers caused # external methods to take precedence over our let definitions - with_isolated_config do |config| - mod = Module.new do - def my_helper - "other_value" - end + mod = Module.new do + def my_helper + "other_value" end - config.include mod - group.class_exec do - let(:my_helper) { "my_value" } - end - expect(group.new.my_helper).to eq "my_value" end + config.include mod + group.class_exec do + let(:my_helper) { "my_value" } + end + expect(group.new.my_helper).to eq "my_value" end end @@ -109,10 +107,8 @@ def my_helper context "when infer_base_class_for_anonymous_controllers is true" do around(:example) do |ex| - with_isolated_config do |config| - config.infer_base_class_for_anonymous_controllers = true - ex.run - end + RSpec.configuration.infer_base_class_for_anonymous_controllers = true + ex.run end it "infers the anonymous controller class" do @@ -129,10 +125,8 @@ def my_helper context "when infer_base_class_for_anonymous_controllers is false" do around(:example) do |ex| - with_isolated_config do |config| - config.infer_base_class_for_anonymous_controllers = false - ex.run - end + RSpec.configuration.infer_base_class_for_anonymous_controllers = false + ex.run end it "sets the anonymous controller class to ApplicationController" do diff --git a/spec/rspec/rails/example/view_example_group_spec.rb b/spec/rspec/rails/example/view_example_group_spec.rb index ec27baf461..f46f7feb04 100644 --- a/spec/rspec/rails/example/view_example_group_spec.rb +++ b/spec/rspec/rails/example/view_example_group_spec.rb @@ -249,21 +249,19 @@ def _view; end # Stub method # Regression test from rspec/rspec-rails#833 it 'is accessible to configuration-level hooks', pending: pending_only_on_ruby_22_rails_52 do - with_isolated_config do - run_count = 0 - RSpec.configuration.before(:each, type: :view) do - # `view` is provided from the view example type, and serves to - # demonstrate this hook is run in the correct context. - allow(view).to receive(:render) { :value } - run_count += 1 - end - group = RSpec::Core::ExampleGroup.describe 'a view', type: :view do - specify { expect(view.render).to eq(:value) } - end - group.run(failure_reporter) - expect(failure_reporter.exceptions).to eq [] - expect(run_count).to eq 1 + run_count = 0 + RSpec.configuration.before(:each, type: :view) do + # `view` is provided from the view example type, and serves to + # demonstrate this hook is run in the correct context. + allow(view).to receive(:render) { :value } + run_count += 1 + end + group = RSpec::Core::ExampleGroup.describe 'a view', type: :view do + specify { expect(view.render).to eq(:value) } end + group.run(failure_reporter) + expect(failure_reporter.exceptions).to eq [] + expect(run_count).to eq 1 end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 05f79e2f09..3f35f58cec 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,6 +23,7 @@ def self.world=(world) I18n.enforce_available_locales = true require 'rspec/support/spec' +require 'rspec/core/sandbox' require 'rspec/rails' require 'ammeter/init' @@ -59,10 +60,16 @@ def self.run_all(reporter = nil) config.warnings = true config.raise_on_warning = true + # Execute a provided block with RSpec global objects (configuration, + # world, current example) reset. This is used to test specs with RSpec. config.around(:example) do |example| - real_world = RSpec.world - RSpec.instance_variable_set(:@world, RSpec::Core::World.new) - example.run - RSpec.instance_variable_set(:@world, real_world) + RSpec::Core::Sandbox.sandboxed do |sandbox_config| + # If there is an example-within-an-example, we want to make sure the inner + # example does not get a reference to the outer example (the real spec) if + # it calls something like `pending`. + sandbox_config.before(:context) { RSpec.current_example = nil } + RSpec::Rails.initialize_configuration(sandbox_config) + example.run + end end end diff --git a/spec/support/generators.rb b/spec/support/generators.rb index ef40590c92..377c44d07a 100644 --- a/spec/support/generators.rb +++ b/spec/support/generators.rb @@ -17,6 +17,7 @@ def setup_default_destination def self.included(klass) klass.extend(Macros) + klass.include(RSpec::Rails::FeatureCheck) end shared_examples_for 'a model generator with fixtures' do |name, class_name| diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb deleted file mode 100644 index bd1d9b0267..0000000000 --- a/spec/support/helpers.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Helpers - include RSpec::Rails::FeatureCheck - - def with_isolated_config - original_config = RSpec.configuration - RSpec.configuration = RSpec::Core::Configuration.new - RSpec::Rails.initialize_configuration(RSpec.configuration) - yield RSpec.configuration - ensure - RSpec.configuration = original_config - end - - RSpec.configure { |c| c.include self } -end diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb index cb424aece9..bfadc8e1f0 100644 --- a/spec/support/shared_examples.rb +++ b/spec/support/shared_examples.rb @@ -27,10 +27,6 @@ def define_group_in(path, group_definition) end end - around(:example) do |ex| - with_isolated_config(&ex) - end - it "adds does not add `:type` metadata on inclusion" do mixin = self.mixin group = RSpec.describe { include mixin }