Test your mailers in Rails 3

I don’t blog very often but I felt this was worth posting, since it left me scratching my head for a few minutes… Trying to test new mailers in Rails 3 (v 3.0.3 to be exact) with no real documentation – everything out there failed miserably since most examples show use of TMail. So here it is, my MailerTest:


require 'test_helper'

class MailerTest < ActionMailer::TestCase

  CHARSET = 'UTF-8'

  context "Forgetting a password" do
    setup do
      @user = User.create(valid_user_params)
      @expected = Mail.new
      @expected.content_type = 'text/plain'
      @expected.charset = CHARSET
      @expected.mime_version = '1.0'
    end

    should "send password reset instructions" do
      @expected.from    = "from@example.com"
      @expected.to      = @user.email
      @expected.subject = "Some Fancy Subject"

      assert_equal @expected, Mailer.password_reset_instructions(@user)
    end
  end
end

I’m using Shoulda with TestUnit, but in theory this should work with rspec, etc. Thanks to http://kpumuk.info/ruby-on-rails/testing-mailers-with-rspec/ for giving me a starting point… There’s definitely more you can add, like testing for the body content, but really this was all I needed for the purposes of this test.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in Development, Rails 3. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>