From 7bf2a05492e7dc3578de7b404bfc9b3e77bb85a4 Mon Sep 17 00:00:00 2001 From: Marcello Barnaba Date: Mon, 23 May 2011 23:57:16 +0200 Subject: [PATCH] Added a body_match that handles, minimally, multipart e-mails --- .../matchers/action_mailer/have_sent_email.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/shoulda/matchers/action_mailer/have_sent_email.rb b/lib/shoulda/matchers/action_mailer/have_sent_email.rb index aaae15e0..39291391 100644 --- a/lib/shoulda/matchers/action_mailer/have_sent_email.rb +++ b/lib/shoulda/matchers/action_mailer/have_sent_email.rb @@ -44,7 +44,7 @@ module Shoulda # :nodoc: def matches?(subject) ::ActionMailer::Base.deliveries.each do |mail| @subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject - @body_failed = !regexp_or_string_match(mail.body, @body) if @body + @body_failed = !body_match(mail, @body) if @body @sender_failed = !regexp_or_string_match_in_array(mail.from, @sender) if @sender @recipient_failed = !regexp_or_string_match_in_array(mail.to, @recipient) if @recipient return true unless anything_failed? @@ -108,6 +108,20 @@ module Shoulda # :nodoc: an_array.include?(a_regexp_or_string) end end + + def body_match(mail, a_regexp_or_string) + # Mail objects instantiated by ActionMailer3 return a blank + # body if the e-mail is multipart. TMail concatenates the + # String representation of each part instead. + if mail.body.blank? && mail.multipart? + mail.parts.select {|p| p.content_type =~ /^text\//}.all? do |part| + regexp_or_string_match(part.body, a_regexp_or_string) + end + else + regexp_or_string_match(mail.body, a_regexp_or_string) + end + end + end end end