[rspec-users] long jumping out of code in specs

Chris Anderson jchris at mfdz.com
Mon Feb 5 01:47:32 EST 2007

I wrote this abomination just now, and wonder if anyone else has had
experience with the pattern behind it. In short, I'm specing an
after_create hook on an ActiveRecord model, that calls a bunch of
private methods. Instead of stubbing all those private methods (which
is verboten anyway, as well as impossible because creating the object
which owns those methods is what I'm specing, so I don't have the
object until I call do_action.)

I'm stubbing a public method that would only get called if
control-flow worked out like it should (the proverbial canary in the
coal mine), and telling it to raise an exception which I catch in my
spec. This way I don't have to bother with either stubbing all the
private methods called in my after_create hook, or making the mock I'm
sending though this gauntlet respond properly to all the calls it

In my case calling RssParser.new is evidence enough that my code
executed like it should.

class SpecShortcut < Exception; end

  specify "should call the standard parsing methods" do

Feeling pain like this while writing specs is usally an indication
that the code could be better designed. But I don't see how else one
could spec ActiveRecord's after_create hook, without going through
with mocking and stubbing all the code the after_create hook uses.
Stopping execution at the point where the expectation is satisfied is
also appealing, in a premature optimization kind of way.

Like I said, an abomination, but maybe a useful one.

Chris Anderson

More information about the rspec-users mailing list