[rspec-users] before before and after after

David Chelimsky dchelimsky at gmail.com
Mon Apr 16 10:29:10 EDT 2007


On 4/16/07, Michael Trier <mtrier at eminentconsultinggroup.com> wrote:
> > 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).

The reason for before(:last) is a situation where you want to be able
to set something up most of the time, but not all of the time. Take a
look at my last comment on
http://rubyforge.org/tracker/?group_id=797&atid=3152&func=detail&aid=9984.
If that doesn't clear it up for you, feel free to ask questions here.

Thx,
David



>
> Michael
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list