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…
#spec/models/user_mailer_spec.rb
describe UserMailer do
fixtures :users
before(:each) do
ActionMailer::Base.deliveries = []
end
it 'should send activation' do
UserMailer.deliver_activation(users(:quentin))
sent.first.subject.should =~ /has been activated/#correct subject
sent.first.body.should =~ /#{CFG[:site]}/#url to our site is there
end
def sent
ActionMailer::Base.deliveries
end
end

6 comments
Comments feed for this article
August 28, 2009 at 21:01
guilherme
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 ?
Thanks,
Guilherme
August 29, 2009 at 5:20
pragmatig
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.
August 29, 2009 at 15:56
Guilherme
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
)
August 29, 2009 at 16:00
pragmatig
It inherits from ActionMailer::Base so its a Mailer :>
September 10, 2009 at 21:34
sandrar
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
October 13, 2009 at 10:42
avsej
You can also check deliveries count
lambda { UserMailer.deliver_notification(mail) }.should change(ActionMailer::Base.deliveries, :count).by(1)