Added a .multipart matcher

This commit is contained in:
Marcello Barnaba 2011-05-23 23:57:16 +02:00 committed by Jason Morrison
parent 7bf2a05492
commit b59af15897
2 changed files with 20 additions and 3 deletions

View File

@ -41,12 +41,18 @@ module Shoulda # :nodoc:
self
end
def multipart(flag = true)
@multipart = !!flag
self
end
def matches?(subject)
::ActionMailer::Base.deliveries.each do |mail|
@subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject
@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
@multipart_failed = mail.multipart? != @multipart if defined?(@multipart)
return true unless anything_failed?
end
@ -78,6 +84,7 @@ module Shoulda # :nodoc:
expectation << " with body #{@body.inspect}" if @body_failed
expectation << " from #{@sender.inspect}" if @sender_failed
expectation << " to #{@recipient.inspect}" if @recipient_failed
expectation << " #{!@multipart && 'not '}being multipart" if @multipart_failed
expectation << "\nDeliveries:\n#{inspect_deliveries}"
end
@ -88,7 +95,7 @@ module Shoulda # :nodoc:
end
def anything_failed?
@subject_failed || @body_failed || @sender_failed || @recipient_failed
@subject_failed || @body_failed || @sender_failed || @recipient_failed || @multipart_failed
end
def regexp_or_string_match(a_string, a_regexp_or_string)

View File

@ -11,8 +11,11 @@ describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
def the_email
mail :from => "do-not-reply@example.com",
:to => "myself@me.com",
:subject => "This is spam",
:body => "Every email is spam."
:subject => "This is spam" do |format|
format.text { render :text => "Every email is spam." }
format.html { render :text => "<h1>HTML is spam.</h1><p>Notably.</p>" }
end
end
end
add_mail_to_deliveries
@ -55,6 +58,13 @@ describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
matcher.failure_message.should =~ /Expected sent email to/
end
it "accepts sent-email when it is multipart" do
should have_sent_email.multipart
matcher = have_sent_email.multipart(false)
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email not being multipart/
end
it "lists all the deliveries within failure message" do
add_mail_to_deliveries