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

Daniel Tenner daniel.ruby at tenner.org
Thu Dec 13 08:57:05 EST 2007

Hi Rick,

Could you paste us what your after_save and your deliver_xyz methods  
look like? I think it would be helpful to make the discussion more  

I've found that whenever my code is hard to spec, it's usually poorly  
designed in the first place, and a better split of responsibilities  
helps make the code both clearer and easily spec'ed.

In this specific case, I'm a bit perplexed that you're sending a pre- 
generated body/subject/sender/etc to the Mailer. Shouldn't that code  
be in the Mailer's deliver_xyz method?

E.g., from my own code:

## password_change_observer.rb
class PasswordChangeObserver < ActiveRecord::Observer
   observe User

   def after_update(user)
     if user.password_modified?


## user_mailer.rb
class UserMailer < ActionMailer::Base

   def new_password(user)
     @subject    = 'Your new password'
     @body       = {"user" => user}
     @recipients = user.email
     @sent_on    = Time.now
     @from       = MAIL_FROM


Dear user,

You have just updated your password. This is a reminder, so that you  
don't forget it.

Your username (email) is: <%= @user.email %>
Your password is: <%= @user.password %>

If you lose this email and forget your password, don't worry - you  
can get a new password
generated by going to http://<%= SERVER_NAME %>/forgot_password/<%=  
@user.id %>"> and entering your email.


The Team


I'm a bit confused because your code seems to say:
msg_creation_parms = {
       :subject => "Subj",
       :body => "hi",
       :sender => people(:rick),
       :recipient => people(:john)

Which would imply you're building the subject, body, sender and  
recipient before passing things on to the mailer? Maybe I got this  
all wrong though...


On 13 Dec 2007, at 13:43 13 Dec 2007, Rick DeNatale wrote:

> On 12/13/07, Daniel Tenner <daniel.ruby at tenner.org> wrote:
>> 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'm not testing the validity of the email, but.
> In this case the email which gets send depends largely on what's in
> the message object.  The mailer extracts attributes from the object to
> create the email, so it's crucial that the correct message is passed
> and that's what I'm trying to spec.
> The actual contents of the message are specified in tests against the
> mailer as I indicated in my earlier reply to Pat.
> -- 
> 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