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

Daniel Tenner daniel.ruby at tenner.org
Thu Dec 13 08:35:03 EST 2007

Hi Rick,

I'm probably a heretic on this point, but I would test  
that :deliver_xyz is being called but not specify what parameters  
it's called with.

What's my reasoning?

- What I'm really testing in the Message spec is not the validity of  
the email that's being sent, but the fact that an email is being  
sent. Basically, I think the Mailer is a different tier.
- I have integration tests that ensure that the system is working as  
a whole. If the params are wrong in a business-meaningful way, those  
should tell me what's wrong
Possibly most important point:
- How is this piece of code likely to break? The most likely way that  
this code would break would be if I change the mailer method so that  
it requires different parameters. Will the spec the way you're  
attempting to write it catch that? Nope. Only the integration test  
will catch that. What will you catch by specifying the exact  
parameters that the mailer is called with, then? Not much - typos in  
the after_create callback, perhaps. As it happens, that would also be  
caught by the integration test.

I like to focus my speccing effort on where the errors are likely to  

As such, my approach would be to simplify the spec to:

describe Message, "from anyone" do

   it "should send an email on save" do
     msg_creation_parms = {
       :subject => "Subj",
       :body => "hi",
       :sender => people(:rick),
       :recipient => people(:john)


But, as I said, I'm probably a heretic :-) Others will probably  
disagree violently (and they'll probably be right).

I hope this helps,


PS: If I decided that the mailer is actually the same tier as the  
model that's calling it, then I would not mock any of it, and do what  
Pat suggested. However, at the moment, I tend to see mailing as a  
separate tier from BL/CRUD.

On 13 Dec 2007, at 12:57 13 Dec 2007, Rick DeNatale wrote:

> On 12/12/07, Pat Maddox <pergesu at gmail.com> wrote:
>> I would not mock the call, and would instead just let the mailer do
>> its thing.  You can verify that a message was sent, match the
>> subject/content, etc.  It's very lightweight so there's no reason not
>> to use it.
> The problem with this is that it combines testing the sending of the
> message with testing how it's rendered.
> I prefer to do the latter in the context of testing the mailer.
> -- 
> Rick DeNatale
> My blog on Ruby
> http://talklikeaduck.denhaven2.com/
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list