[rspec-users] before before and after after

Michael Trier mtrier at eminentconsultinggroup.com
Mon Apr 16 10:24:53 EDT 2007

> Hi all,
> There are currently a number of ways to extend RSpec that are
> documented inconsistently and work in sometimes conflicting and/or
> confusing ways. I'd like to resolve that. The recent addition of
> Spec::Runner.configure is part of that resolution.
> 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:
> 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
>   it "should ..." do
>     #this is the example
>   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
> In addition to this, there have been requests to support global
> "before" and "after". Here's where I'd like to end up:
> Spec::DSL::Example.before(:first) do
>   # before "before(:all)" and "before(:each)"
> end
> Spec::DSL::Example.before(:last) do
>   # after "before(:all)" and "before(:each)", but before the example
> end
> Spec::DSL::Example.after(:first) do
>   # before "after(:each)" and "after(:all)", but after the example
> end
> Spec::DSL::Example.after(:last) do
>   # after "after(:each)" and "after(:all)"
> end
> 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. In fact, you may not use
> any of them if you prefer the Object Mother approach (helper methods
> that create the state you want to start with). But this would give you
> a lot of choice at both global and granular levels.
> WDYT? Too confusing? Too limiting? Just right?

It sounds good to me, although I can't see a situation where I would  
need before(:last) or after(:first), just the outerlying ones (before 
(:first) and after(:last).


More information about the rspec-users mailing list