Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit ab7cc1c

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 4698745 commit ab7cc1c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
### Development
2+
3+
Bug Fixes:
4+
5+
* Correctly run `before(:suite)` (and friends) in the context of an example
6+
group, making the expected RSpec environment available. (Jon Rowe, #1986)
7+
18
### 3.3.0 / 2015-06-12
29
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.2.3...v3.3.0)
310

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ 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) { double('something'); run = true }
36+
RSpec.configuration.with_suite_hooks { }
37+
expect(run).to be true
38+
end
39+
40+
it 'allows access to rspec-expectation methods within the hook' do
41+
RSpec.configuration.__send__(registration_method, :suite) { expect(true).to be false }
42+
expect {
43+
RSpec.configuration.with_suite_hooks { }
44+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
45+
end
46+
2647
context "registered on an example group" do
2748
it "is ignored with a clear warning" do
2849
sequence = []

0 commit comments

Comments
 (0)