Skip to content

Commit 1285617

Browse files
committed
run suite hooks in an example group context
All other types of hooks are run in the context of example group during an individual example group or examples run (respectively), as a suite hook uses a special special sub class of example to run in it wasn't even being setup correctly with an example group so they ran in the context of nil. fixed.
1 parent 43e62a2 commit 1285617

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)