Posts Tagged ‘test’

Test ActionMailer for a multipart mail

Thursday, November 22nd, 2007

When your ActionMailer creates multipart mails with both html et text bodies, your unit test can become a nightmare. The default policy to test ActionMailer (provided by the generated testcase) is comparing the created mail content with a static fixture. It’s very difficult to maintain. And this approach is impossible with a multipart mail. Several things in the encoded mail are randomly created (like the mimepart boundaries).

Another policy is promoted by Dave Thomas in his book Agile Web Development with Rails or Shanti A. Braford. The principle is simple : test only that the important information is present into the mail body.

This approach is meanful in a html/text mail. The two parts contain often the same important information. So the same assertions can be performed on the two parts of the mail :

sent.parts.each do |part|
  assert_match #{user.name}, part.body, "#{part.content_type} invalid"
  ...
end