[rspec-users] thoughts on mocks and specs

Chris Anderson jchris at mfdz.com
Mon Jan 8 00:55:01 EST 2007

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')

  body do

  specify "should run the body block after every specification" do


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.


Chris Anderson

More information about the rspec-users mailing list