shared_context before(:each) happens before config.before(:each) #632

Closed
bradrobertson opened this Issue Jun 10, 2012 · 11 comments

Projects

None yet

5 participants

@bradrobertson

This, in my opinion, is not the correct behaviour. I've illustrated that it is in fact the case in this repo.

I would have thought that config.before(:each) is run first, then the shared_context before(:each), and finally the before(:each) defined in the example group itself.

Running this test yield this output:

$ rspec spec
before suite in spec_helper
before all in spec_helper
before each in shared context
before each in spec_helper
before each in test
.

Finished in 0.00241 seconds
1 example, 0 failures

Is this on purpose?

I ask because I'm using config.before(:each) to start a transaction (with DatabaseCleaner) so that all test data will be cleaned up after each test, but obviously in this case, any data populated in the shared_context would not be created within that transaction.

@dchelimsky
Member

It's a bug that has to do with how shared groups get included via metadata. Until it's fixed, you can get the behavior you want by including the shared context with the include_context method instead of using metadata to create the hooks.

@bradrobertson

gotcha... thanks david.

@dchelimsky
Member

For my future self (or anyone else interested in helping resolve this): the problem is that rspec-core is piggy backing on config.extend to include shared content via metadata. By design, modules are added to groups via include or extend before any content is evaluated. Basically that feature was added to extend the behavior of a group (i.e. what functionality it supports), not to add before/after hooks.

To resolve this we should probably use a different mechanism for these shared groups.

@gja gja added a commit to c42engineering/rspec-core that referenced this issue Jun 12, 2012
@gja gja Pending spec for shared context that exposes Issue #632 5fc19b6
@JonRowe
Member
JonRowe commented Mar 7, 2013

Did this ever get resolved? Or is it still an issue...

@myronmarston
Member

Not yet. Want to take a stab at it?

@JonRowe
Member
JonRowe commented Mar 7, 2013

I'll put it on the list of things to look at ;)

@michihuber
Contributor

@JonRowe, are you working on this already? I could take a look otherwise.

@JonRowe
Member
JonRowe commented Mar 20, 2013

I haven't gotten around to it yet, so it's all yours :)

@michihuber michihuber added a commit to michihuber/rspec-core that referenced this issue Mar 25, 2013
@gja @michihuber gja + michihuber Pending spec for shared context that exposes Issue #632 7ff0e81
@michihuber
Contributor

The issue itself can be fixed by registering hooks before configuring the example group:
michihuber@7a88e87#L0L286

Of course, shared_examples are still piggy backing on config.extend when included through metadata. Do you prefer the small fix or should I try to factor it into it's own inclusion mechanism to make it more transparent?

@myronmarston
Member

@michihuber -- that looks like a pretty simple fix. I care mostly just that we get it fixed; the how is less important, and I tend to favor simple solutions like these. Want to submit a PR?

@myronmarston
Member

Fixed by #845.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment