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

Make rspec-expectations and rspec-mocks APIs available in before(:suite) hooks #1986

Merged
merged 1 commit into from
Jun 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Friends" implies plural but it's really just after(:suite), right? Might as well say:

Correctly run before(:suite) and after(:suite) hooks in the context of an example group...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, technically they are run in the context of an instance of an example group, not an example group. Users are often confused by this so it's helpful if we're accurate in our changelog entries :).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would still require the user calling with_temporary_scope before being able to use mocks correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It's explicitly documented that that's required if you ever want to use rspec-mocks outside the scope of a single example:

https://relishapp.com/rspec/rspec-mocks/v/3-3/docs/basics/scope

This bug fix doesn't affect with_temporary_scope in any way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 😸

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are multiple (before, after, prepend_before, append_before, prepend_after, append_after) but I'll change it if you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I forgot about those.

It's fine how it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the example group vs instance of the example group thing still matters, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I always do too, but I was reminded by the spec file :) (it loops through all of them)

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth adding some lines to this (or perhaps making a new spec) showing that rspec-mocks and rspec-expectations APIs can be used from within the hook. Ultimately that's the bug we're trying to fix. l think that as rspec-core currently works, any block evaluated in the context of an ExampleGroup instance would support those APIs but there's no guarantee that will always be true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below :)


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