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

Rick DeNatale rick.denatale at gmail.com
Wed Dec 12 22:09:06 EST 2007


I've got a model Message, which needs to send an email using action
mailer after it's first saved in the database.

I want to pass the model to the mailer which then uses methods on the
message model to render the email.

So the natural way to do this is in an after_create callback on the
Message model.

But I can't see an easy way to test this. Here's my spec

describe Message, "from anyone" do

  it "should be sent on save" do
    msg_creation_parms = {
      :subject => "Subj",
      :body => "hi",
      :sender => people(:rick),
      :recipient => people(:john)
      }
    SantasMailbox.should_receive(:deliver_secret_santa).with(Message.new(msg_creation_parms))
    Message.create(msg_creation_parms)
  end

end

This fails, but only because the model object has an id and time
stamps assigned as it's saved.

Spec::Mocks::MockExpectationError in 'Message from anyone should be
sent on save'
Mock 'Class' expected :deliver_secret_santa with (#<Message id: nil,
subject: "Subj", body: "hi", sender_id: 343839476, recipient_id:
21341157, message_type: nil, created_at: nil, updated_at: nil>) but
received it with (#<Message id: 1, subject: "Subj", body: "hi",
sender_id: 343839476, recipient_id: 21341157, message_type: nil,
created_at: "2007-12-12 21:53:16", updated_at: "2007-12-12 21:53:16">)

I figured I'd through this out to the list for ideas on how best to
approach this before I go to bed and sleep on it. <G>

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/


More information about the rspec-users mailing list