Wednesday, October 10, 2012

Test email deliveries on Rails

In config/environments/test.rb, set "config.action_mailer.delivery_method = :test".  It tell action mailer not to deliver emails to the real world.  Instead, emails will be accumulates in the ActionMailer::Base.deliveries array.

So, in email test, do something like:

test "should send email" do
    assert_difference "ActionMailer::Base.deliveries.size" do
        ...
    end
    last_email = ActionMailer::Base.deliveries.last
    assert_equal, ..., last_email.from
    assert_equal, ..., last_email.subject
    ...
end

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.