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

Rick DeNatale rick.denatale at gmail.com
Thu Dec 13 09:44:37 EST 2007

On 12/13/07, Daniel Tenner <daniel.ruby at tenner.org> wrote:
> 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
> concrete.
> 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?

In this case the bulk of the message is coming from, surprise, the
message object.

This is a secret santa site, the idea is to allow folks to communicate
without giving away who is who.

class Message < ActiveRecord::Base

  belongs_to :sender, :class_name => "Person", :foreign_key => :sender_id
  belongs_to :recipient, :class_name => "Person", :foreign_key => :recipient_id

  after_create do |msg|
    puts "\n**** after_create #{msg}\n#{caller.join("\n")}\n"


class SantasMailbox < ActionMailer::Base
  def secret_santa(message)
    subject     message.subject
    body(        {:sender => message.sender_name,
                     :recipient => message.recipient_first_name, :text => body})
    recipients  "#{recipient.full_name} <#{recipient.email}>"
    from        "#{message.sender_full_name}
    sent_on     message.created_at
    content_type 'text/html'
    headers     {}

Where methods like sender_name etc. on the Message object produce
appropriate clear or obfuscated (e.g. 'Your Secret Santa'  depending
on the message state.

Rick DeNatale

My blog on Ruby

More information about the rspec-users mailing list