[rspec-users] before before and after after

nicholas a. evans nick at ekenosen.net
Mon Apr 16 20:45:05 EDT 2007


On 4/16/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> The thing I want to tackle now is the stuff that happens before and
> after each example. Currently (in trunk), things are executed in the
> following order:
...
> So the over all order for every example (a.k.a. specify) would be:
>
> before(:first)
> before(:all) #once per "describe"
> before(:each)
> before(:last)
> example
> after(:first)
> after(:each)
> after(:all) #once per "describe"
> after(:last)
>
> I'm certainly open to other suggestions - this is merely a proposal.
> In most cases you wouldn't use all of these.
...
> WDYT? Too confusing? Too limiting? Just right?

I usually would not have a need for any of them except for
before(:each).  I like the new syntax though. "before(:all)" and
"before(:each)" seem better than "context_setup" and "setup".  And by
declaring all of them, it feels consistent and somewhat intuitive, as
well as very customizable.  I like it.


Slightly tangential, but I hope not to much so...
I am curious though if this could be made to work with my earlier
suggestion: http://rubyforge.org/pipermail/rspec-users/2007-April/001180.html

Specifically, a way of running a specific action ("example"?) for each
expectation, but sometimes run it before and sometimes after
(depending upon whether the expectation is a mock setup or a state
based check).  The way I wrote it before, I presented two new
scenarios... but one of those scenarios could also be the default.


describe Thing do
 before(:all) do
   #happens first, but only once within this "describe" block
 end
 before(:each) do
   #happens before each example within this "describe" block
 end
 for_each do
  # happens before each "it" example within this "describe" block
 end
 it "should ..." do
   #this is an expectation about the state after the "for_each" block
 end
 after(:each) do
   #happens after each example within this "describe" block
 end
 after(:all) do
   #happens last, but only once within this "describe" block
 end
end

or

describe Thing do
 before(:all) do
   #happens first, but only once within this "describe" block
 end
 before(:each) do
   #happens before each example within this "describe" block
 end
 expect_that_it "should ..." do
   #this is the mock expectation
 end
 for_each do
  # happens after each "expect_that_it" example within this "describe" block
 end
 after(:each) do
   #happens after each example within this "describe" block
 end
 after(:all) do
   #happens last, but only once within this "describe" block
 end
end

Basically, the "for_each" block runs before each "it 'should...'"
expectation, and after each "expect_that_it 'should...'"  mock
expectation.

I think that this could significantly increase the readability of
specs that use a lot of mock expectations, as well as DRY up the style
proposed here:
http://blog.davidchelimsky.net/articles/2006/11/09/tutorial-rspec-stubs-and-mocks

But it might also make things more confusing... and I'm really not
sure that "for_each" is the right phrase to describe the block (I've
been using "action" up until now, and that doesn't feel quite right
either).  I'm I chasing a bad approach here?

-- 
Nick


More information about the rspec-users mailing list