RSpec: Testing Mail Delivery (simple)

To really test mail delivery one needs to send and then grab via smtp, but i do not want to go this far atm. So i wrote a surprisingly simple test, that satisfies my need for coverage and be done with it!

If you messed around with your environment.rb and are not sure wheter emails are send in test mode, just enter a real email and watch the inbox, before sending mass-emails…

describe UserMailer do
  fixtures :users

  before(:each) do
    ActionMailer::Base.deliveries = []

  it 'should send activation' do
    sent.first.subject.should =~ /has been activated/#correct subject
    sent.first.body.should =~ /#{CFG[:site]}/#url to our site is there

  def sent

6 thoughts on “RSpec: Testing Mail Delivery (simple)

  1. Hi..
    How do you treat your e-mails like signup/activation? you make a Observer and use callbacks OR this UserMailer is a separeted class OR ActiveRecord::Base inherited ?


  2. I use 2 different approaches atm(in different projects):
    1: UserObserver calls UserMailer.deliver_signup
    2: User calls send_email after_create

    Calling it directly in the model is a bit simpler imo.

  3. Thanks, i agree with you that is simple.
    I’ve just thought about how was made this UserMailer, if it’s ActiveRecord::Base inherited or a simple class that send e-mails..

    congratulations for your posts. keep blogging, i really like your blog :))

  4. You can also check deliveries count

    lambda { UserMailer.deliver_notification(mail) }.should change(ActionMailer::Base.deliveries, :count).by(1)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s