Tip for Testing Email with Ruby on Rails and Shoulda

by Justin Ball on August 18th, 2008

Shoulda comes with a great method for testing email called 'assert_sent_email'. (If you try it and get a method missing error update Shoulda. Not that anyone wouldn't know to just do that). No matter what you use to test email as long as you are in test mode your emails will be placed into an array accessible using:

ActionMailer::Base.deliveries

This is due to this line in test.rb found in the environments folder:

config.action_mailer.delivery_method = :test

Now here's the tip. You might think you are really smart if you take advantage of the fact that Rails has a very handy initializers folder and put a file in there called mail.rb with your mail configuration.

DON'T do it. The initializer will override the value set in the test.rb file and your tests will fail.

  • I think you can do stuff like this:
    ActionMailer::Base.deliveries[0]
  • Pierre-Alexandre
    thanks
    ActionMailer::Base.deliveries
    was what i need.

    do you kwno if it's possible to get the instance value on the mail
    ex:

    stuf: blablabla

    I try
    ActionMailer::Base.deliveries.last.stuf
    but it didn't work :(
  • I've forgotten this a few times, and subsequently came across this post. You deserve at least my thanks. Thanks!
blog comments powered by Disqus