Skip to content

Commit

Permalink
run suite hooks in an example group context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JonRowe committed Jun 18, 2015
1 parent ae05642 commit 53a598b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Expand Up @@ -5,6 +5,12 @@ Enhancements:
* Combine multiple `--pattern` arguments making them equivalent to
`--pattern=1,2,...,n`. (Jon Rowe, #2002)

Bug Fixes:

* Correctly run `before(:suite)` (and friends) in the context of an example
group instance, thus making the expected RSpec environment available.
(Jon Rowe, #1986)

### 3.3.0 / 2015-06-12
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.2.3...v3.3.0)

Expand Down
1 change: 1 addition & 0 deletions lib/rspec/core/example.rb
Expand Up @@ -558,6 +558,7 @@ def issue_deprecation(_method_name, *_args)
class SuiteHookContext < Example
def initialize
super(AnonymousExampleGroup, "", {})
@example_group_instance = AnonymousExampleGroup.new
end

# rubocop:disable Style/AccessorMethodName
Expand Down
24 changes: 24 additions & 0 deletions spec/rspec/core/suite_hooks_spec.rb
Expand Up @@ -23,6 +23,30 @@ module RSpec::Core
}.to raise_error(ZeroDivisionError)
end

it 'runs in the context of an example group' do
run_context = nil
RSpec.configuration.__send__(registration_method, :suite) { run_context = self }
RSpec.configuration.with_suite_hooks { }
expect(run_context).to be_a ExampleGroup
end

it 'allows access to rspec-mocks methods within the hook' do
run = false
RSpec.configuration.__send__(registration_method, :suite) do
RSpec::Mocks.with_temporary_scope { double('something') }
run = true
end
RSpec.configuration.with_suite_hooks { }
expect(run).to be true
end

it 'allows access to rspec-expectation methods within the hook' do
RSpec.configuration.__send__(registration_method, :suite) { expect(true).to be false }
expect {
RSpec.configuration.with_suite_hooks { }
}.to raise_error RSpec::Expectations::ExpectationNotMetError
end

context "registered on an example group" do
it "is ignored with a clear warning" do
sequence = []
Expand Down

0 comments on commit 53a598b

Please sign in to comment.