Skip to content

Commit 45492fd

Browse files
authored
Merge pull request #2365 from rspec/sync-worlds
Get rid of the code poking around RSpec's internals
2 parents 7643f3b + 2c23692 commit 45492fd

File tree

6 files changed

+36
-54
lines changed

6 files changed

+36
-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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def self.world=(world)
2323
I18n.enforce_available_locales = true
2424

2525
require 'rspec/support/spec'
26+
require 'rspec/core/sandbox'
2627
require 'rspec/rails'
2728
require 'ammeter/init'
2829

@@ -59,10 +60,16 @@ def self.run_all(reporter = nil)
5960
config.warnings = true
6061
config.raise_on_warning = true
6162

63+
# Execute a provided block with RSpec global objects (configuration,
64+
# world, current example) reset. This is used to test specs with RSpec.
6265
config.around(:example) do |example|
63-
real_world = RSpec.world
64-
RSpec.instance_variable_set(:@world, RSpec::Core::World.new)
65-
example.run
66-
RSpec.instance_variable_set(:@world, real_world)
66+
RSpec::Core::Sandbox.sandboxed do |sandbox_config|
67+
# If there is an example-within-an-example, we want to make sure the inner
68+
# example does not get a reference to the outer example (the real spec) if
69+
# it calls something like `pending`.
70+
sandbox_config.before(:context) { RSpec.current_example = nil }
71+
RSpec::Rails.initialize_configuration(sandbox_config)
72+
example.run
73+
end
6774
end
6875
end

spec/support/generators.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def setup_default_destination
1717

1818
def self.included(klass)
1919
klass.extend(Macros)
20+
klass.include(RSpec::Rails::FeatureCheck)
2021
end
2122

2223
shared_examples_for 'a model generator with fixtures' do |name, class_name|

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)