Skip to content

Commit d4090e8

Browse files
committed
Get rid of the code poking around RSpec internals
Our previous code was prone to out-of-sync worlds: with_isolated_config do RSpec::Core::ExampleGroup.describe('a view') { it { } } RSpec.configuration.world.example_groups # => [] end
1 parent 60dc9c4 commit d4090e8

File tree

5 files changed

+37
-54
lines changed

5 files changed

+37
-54
lines changed

spec/rspec/rails/example/controller_example_group_spec.rb

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ def group_for(klass)
3131
# before #738 implicit subject definition for controllers caused
3232
# external methods to take precedence over our let definitions
3333

34-
with_isolated_config do |config|
35-
mod = Module.new do
36-
def my_helper
37-
"other_value"
38-
end
34+
mod = Module.new do
35+
def my_helper
36+
"other_value"
3937
end
40-
config.include mod
41-
group.class_exec do
42-
let(:my_helper) { "my_value" }
43-
end
44-
expect(group.new.my_helper).to eq "my_value"
4538
end
39+
config.include mod
40+
group.class_exec do
41+
let(:my_helper) { "my_value" }
42+
end
43+
expect(group.new.my_helper).to eq "my_value"
4644
end
4745
end
4846

@@ -109,10 +107,8 @@ def my_helper
109107

110108
context "when infer_base_class_for_anonymous_controllers is true" do
111109
around(:example) do |ex|
112-
with_isolated_config do |config|
113-
config.infer_base_class_for_anonymous_controllers = true
114-
ex.run
115-
end
110+
RSpec.configuration.infer_base_class_for_anonymous_controllers = true
111+
ex.run
116112
end
117113

118114
it "infers the anonymous controller class" do
@@ -129,10 +125,8 @@ def my_helper
129125

130126
context "when infer_base_class_for_anonymous_controllers is false" do
131127
around(:example) do |ex|
132-
with_isolated_config do |config|
133-
config.infer_base_class_for_anonymous_controllers = false
134-
ex.run
135-
end
128+
RSpec.configuration.infer_base_class_for_anonymous_controllers = false
129+
ex.run
136130
end
137131

138132
it "sets the anonymous controller class to ApplicationController" do

spec/rspec/rails/example/view_example_group_spec.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,19 @@ def _view; end # Stub method
249249

250250
# Regression test from rspec/rspec-rails#833
251251
it 'is accessible to configuration-level hooks', pending: pending_only_on_ruby_22_rails_52 do
252-
with_isolated_config do
253-
run_count = 0
254-
RSpec.configuration.before(:each, type: :view) do
255-
# `view` is provided from the view example type, and serves to
256-
# demonstrate this hook is run in the correct context.
257-
allow(view).to receive(:render) { :value }
258-
run_count += 1
259-
end
260-
group = RSpec::Core::ExampleGroup.describe 'a view', type: :view do
261-
specify { expect(view.render).to eq(:value) }
262-
end
263-
group.run(failure_reporter)
264-
expect(failure_reporter.exceptions).to eq []
265-
expect(run_count).to eq 1
252+
run_count = 0
253+
RSpec.configuration.before(:each, type: :view) do
254+
# `view` is provided from the view example type, and serves to
255+
# demonstrate this hook is run in the correct context.
256+
allow(view).to receive(:render) { :value }
257+
run_count += 1
258+
end
259+
group = RSpec::Core::ExampleGroup.describe 'a view', type: :view do
260+
specify { expect(view.render).to eq(:value) }
266261
end
262+
group.run(failure_reporter)
263+
expect(failure_reporter.exceptions).to eq []
264+
expect(run_count).to eq 1
267265
end
268266
end
269267

spec/spec_helper.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Application < ::Rails::Application
1515
I18n.enforce_available_locales = true
1616

1717
require 'rspec/support/spec'
18+
require 'rspec/core/sandbox'
1819
require 'rspec/rails'
1920
require 'ammeter/init'
2021

@@ -51,10 +52,18 @@ def self.run_all(reporter = nil)
5152
config.warnings = true
5253
config.raise_on_warning = true
5354

55+
# Execute a provided block with RSpec global objects (configuration,
56+
# world, current example) reset. This is used to test specs with RSpec.
5457
config.around(:example) do |example|
55-
real_world = RSpec.world
56-
RSpec.instance_variable_set(:@world, RSpec::Core::World.new)
57-
example.run
58-
RSpec.instance_variable_set(:@world, real_world)
58+
RSpec::Core::Sandbox.sandboxed do |sandbox_config|
59+
# If there is an example-within-an-example, we want to make sure the inner
60+
# example does not get a reference to the outer example (the real spec) if
61+
# it calls something like `pending`.
62+
sandbox_config.before(:context) { RSpec.current_example = nil }
63+
RSpec::Rails.initialize_configuration(sandbox_config)
64+
example.run
65+
end
5966
end
67+
68+
config.include RSpec::Rails::FeatureCheck
6069
end

spec/support/helpers.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

spec/support/shared_examples.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ def define_group_in(path, group_definition)
2727
end
2828
end
2929

30-
around(:example) do |ex|
31-
with_isolated_config(&ex)
32-
end
33-
3430
it "adds does not add `:type` metadata on inclusion" do
3531
mixin = self.mixin
3632
group = RSpec.describe { include mixin }

0 commit comments

Comments
 (0)