Skip to content

Commit 16d3b92

Browse files
committed
Merge pull request rspec#1986 from rspec/run_suite_hooks_in_an_example_group
Make rspec-expectations and rspec-mocks APIs available in `before(:suite)` hooks
2 parents 43e62a2 + 1285617 commit 16d3b92

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Enhancements:
55
* Combine multiple `--pattern` arguments making them equivalent to
66
`--pattern=1,2,...,n`. (Jon Rowe, #2002)
77

8+
Bug Fixes:
9+
10+
* Correctly run `before(:suite)` (and friends) in the context of an example
11+
group instance, thus making the expected RSpec environment available.
12+
(Jon Rowe, #1986)
13+
814
### 3.3.0 / 2015-06-12
915
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.2.3...v3.3.0)
1016

lib/rspec/core/example.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ def issue_deprecation(_method_name, *_args)
558558
class SuiteHookContext < Example
559559
def initialize
560560
super(AnonymousExampleGroup, "", {})
561+
@example_group_instance = AnonymousExampleGroup.new
561562
end
562563

563564
# rubocop:disable Style/AccessorMethodName

spec/rspec/core/suite_hooks_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ module RSpec::Core
2323
}.to raise_error(ZeroDivisionError)
2424
end
2525

26+
it 'runs in the context of an example group' do
27+
run_context = nil
28+
RSpec.configuration.__send__(registration_method, :suite) { run_context = self }
29+
RSpec.configuration.with_suite_hooks { }
30+
expect(run_context).to be_a ExampleGroup
31+
end
32+
33+
it 'allows access to rspec-mocks methods within the hook' do
34+
run = false
35+
RSpec.configuration.__send__(registration_method, :suite) do
36+
RSpec::Mocks.with_temporary_scope { double('something') }
37+
run = true
38+
end
39+
RSpec.configuration.with_suite_hooks { }
40+
expect(run).to be true
41+
end
42+
43+
it 'allows access to rspec-expectation methods within the hook' do
44+
RSpec.configuration.__send__(registration_method, :suite) { expect(true).to be false }
45+
expect {
46+
RSpec.configuration.with_suite_hooks { }
47+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
48+
end
49+
2650
context "registered on an example group" do
2751
it "is ignored with a clear warning" do
2852
sequence = []

0 commit comments

Comments
 (0)