[rspec-users] "Tricks" for testing after_create callback???

Rick DeNatale rick.denatale at gmail.com
Thu Dec 13 11:39:16 EST 2007

On 12/13/07, Rick DeNatale <rick.denatale at gmail.com> wrote:
> On 12/13/07, David Chelimsky <dchelimsky at gmail.com> wrote:

> > This is a bit scary :(
> >
> > Rick - would you mind sticking this information, a bit filtered, into
> > the tracker?
> Well, as I was doing just that, I looked a bit closer at those
> walkbacks, and it occurred to me that I was assuming that rspec was
> calling the method twice.
> As it turns out, it was actually ActiveRecord calling the callback
> twice, and it went away when I changed from
> after_create do |msg|
>    ...
> end
> to
> def after_create
>    ...
> end
> So I guess I need to follow up on Rails core!  Although that change
> fixed things for this case.

Okay,  more info.  As I was investigating this for a possible ticket
for Rails Trac, it dawned on me that maybe, just maybe, the
after_create do...end was being executed twice.  So I added a puts
"**** loading Message" in the class definition for the Message AR
class, and sure enough it was being loaded twice!

It turns out that I had

require 'message'

at the top of the spec file. and a few other descriptions in the same
file which were using the messages fixture.

When I remove the require 'message' from my spec file, it seems to
work with either form of the callback definition.

So I'm not sure if this is an Rspec bug, an AR fixtures bug, both or
neither. <G?>
Rick DeNatale

My blog on Ruby

More information about the rspec-users mailing list