-
-
Notifications
You must be signed in to change notification settings - Fork 753
Make rspec-expectations and rspec-mocks APIs available in before(:suite)
hooks
#1986
Conversation
^^ found and done |
RSpec.configuration.__send__(registration_method, :suite) { run_context = self } | ||
RSpec.configuration.with_suite_hooks { } | ||
expect(run_context).to be_a ExampleGroup | ||
end |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below :)
e719b40
to
ab7cc1c
Compare
|
||
it 'allows access to rspec-mocks methods within the hook' do | ||
run = false | ||
RSpec.configuration.__send__(registration_method, :suite) { double('something'); run = true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised this works...I assume you'd have to wrap the double
call in a with_temporary_scope
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? They're not using the proxy space so they're harmless on their own right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see why it works now...
- A bare double with no allowances or expectations does not trigger the
The use of doubles or partial doubles from rspec-mocks outside of the per-test lifecycle is not supported. (RSpec::Mocks::OutsideOfExampleError)
error. Sodouble('foo')
is OK. - Even if you change to
double('foo', :msg => "val")
it still passes because there is an active rspec-mocks scope here since this is running inside and rspec example.
For a before(:suite)
hook declared in spec_helper
, double('foo', :msg => "val")
does fail as I would expect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want me to add a temporary scope? As this is currently demoing just access to the api I'm ok with this as is..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't care much. Mostly I was surprised/confused by it. However, given I as surprised by it and rspec-mocks features don't work in :suite
hooks normally without the scope, I think it's worth adding it to prevent future confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
LGTM |
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.
ab7cc1c
to
53a598b
Compare
LGTM |
Make rspec-expectations and rspec-mocks APIs available in `before(:suite)` hooks
Should we backport this to 3-3-maintenance? |
Done. |
…e_group Make rspec-expectations and rspec-mocks APIs available in `before(:suite)` hooks
…hooks_in_an_example_group This commit was imported from rspec/rspec-core@45f2cd9.
Currently users get errors if they try to use rspec-expectations or rspec-mocks APIs (e.g.
expect(x).to y
orallow(x).to receive
) in abefore
orafter
:suite
hook. It's not a common need (and not something I've ever wanted!) but for consistency/parity with the other hook types they should be available. It looks like the hooks are intended to run in the context of aSuiteHookContext
object but somehow it's actually being run in the context ofnil
(woops!).See rspec/rspec-mocks#966 for an example use case.