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
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
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.
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 :))
It inherits from ActionMailer::Base so its a Mailer :>
Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.
You can also check deliveries count
lambda { UserMailer.deliver_notification(mail) }.should change(ActionMailer::Base.deliveries, :count).by(1)