[rspec-users] thoughts on mocks and specs

David Chelimsky dchelimsky at gmail.com
Mon Jan 8 10:02:12 EST 2007


On 1/8/07, Pat Maddox <pergesu at gmail.com> wrote:
> On 1/7/07, Chris Anderson <jchris at mfdz.com> wrote:
> > I spent the last couple of days getting my sea legs with Rails and
> > RSpec. I'd been waiting til things seemed calmer before jumping in,
> > and I'm overall very happy with my experience so far.
> >
> > My only real annoyance so far has been forgetting to call "do_post" or
> > "do_create" from my specify blocks. My mocks don't get the calls they
> > want, and it usually isn't until I've mucked around in my application
> > code for a bit that I realize the real problem.
> >
> > Wouldn't it be nice to be able to define a callback that gets run
> > after every specify block? Maybe it would look like this:
> >
> > context "A leaner syntaxed context" do
> >
> >   setup do
> >     @obj = mock('obj')
> >   end
> >
> >   body do
> >     @obj.whatever
> >   end
> >
> >   specify "should run the body block after every specification" do
> >     @obj.should_receive(:whatever)
> >   end
> >
> > end
> >
> > I know sometimes one might want to put some verification code after
> > the body. In that case, you could allow an explicit call to body from
> > within the specify block, which would preclude it from being called
> > again at the end.
> >
> > It seems to me that this feature isn't just a fight for DRY, but also
> > encourages good spec design. Situations diverse enough to need
> > separate body code could use different contexts anyway.
> >
> > If there is a call for this feature, I'd be happy to make a go at
> > patching it into RSpec.
> >
> > Reactions?
> >
> > --
> > Chris Anderson
> > http://jchris.mfdz.com
>
> You can do that using teardown.

You could, yes, but that means that part of your spec is in teardown.
To me, setup and teardown are for setting up and tearing down an
environment in which you'll execute the specs. Each spec should tell
it's whole story. You shouldn't have to look at setup or teardown to
understand what is being specified, though you may need to look there
to understand failures.

Also, sometimes the do_blah needs to come after the expectations (when
you're using mocks), and sometimes before, so we need some way to say
"execute the request before OR after each specify block". Seems to me
we're opening up a can of worms here. Simpler to just put it in the
spec block.

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


More information about the rspec-users mailing list