Add bcc and cc functionality to the HaveSentEmailMatcher

This commit is contained in:
Stafford Brunk 2011-05-27 09:49:25 -04:00
parent 224f2e5a70
commit 4e199e085e
2 changed files with 85 additions and 2 deletions

View File

@ -40,6 +40,26 @@ module Shoulda # :nodoc:
@recipient = recipient
self
end
def cc(recipient)
@cc = recipient
self
end
def with_cc(recipients)
@cc_recipients = recipients
self
end
def bcc(recipient)
@bcc = recipient
self
end
def with_bcc(recipients)
@bcc_recipients = recipients
self
end
def matches?(subject)
::ActionMailer::Base.deliveries.each do |mail|
@ -47,6 +67,10 @@ module Shoulda # :nodoc:
@body_failed = !regexp_or_string_match(mail.body, @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
@cc_failed = !regexp_or_string_match_in_array(mail.cc, @cc) if @cc
@cc_recipients_failed = !match_array_in_array(mail.cc, @cc_recipients) if @cc_recipients
@bcc_failed = !regexp_or_string_match_in_array(mail.bcc, @bcc) if @bcc
@bcc_recipients_failed = !match_array_in_array(mail.bcc, @bcc_recipients) if @bcc_recipients
return true unless anything_failed?
end
@ -67,6 +91,10 @@ module Shoulda # :nodoc:
description << " containing #{@body.inspect}" if @body
description << " from #{@sender.inspect}" if @sender
description << " to #{@recipient.inspect}" if @recipient
description << " cc #{@cc.inspect}" if @cc
description << " with cc #{@cc_recipients.inspect}" if @cc_recipients
description << " bcc #{@bcc.inspect}" if @bcc
description << " with bcc #{@bcc_recipients.inspect}" if @bcc_recipients
description
end
@ -78,6 +106,10 @@ 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 << " cc #{@cc.inspect}" if @cc_failed
expectation << " with cc #{@cc_recipients.inspect}" if @cc_recipients_failed
expectation << " bcc #{@bcc.inspect}" if @bcc_failed
expectation << " with bcc #{@bcc_recipients.inspect}" if @bcc_recipients_failed
expectation << "\nDeliveries:\n#{inspect_deliveries}"
end
@ -88,7 +120,7 @@ module Shoulda # :nodoc:
end
def anything_failed?
@subject_failed || @body_failed || @sender_failed || @recipient_failed
@subject_failed || @body_failed || @sender_failed || @recipient_failed || @cc_failed || @cc_recipients_failed || @bcc_failed || @bcc_recipients_failed
end
def regexp_or_string_match(a_string, a_regexp_or_string)
@ -108,6 +140,13 @@ module Shoulda # :nodoc:
an_array.include?(a_regexp_or_string)
end
end
def match_array_in_array(target_array, match_array)
target_array.sort!
match_array.sort!
target_array.each_cons(match_array.size).include? match_array
end
end
end
end

View File

@ -12,6 +12,8 @@ describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
mail :from => "do-not-reply@example.com",
:to => "myself@me.com",
:subject => "This is spam",
:cc => ["you@you.com", "joe@bob.com", "hello@goodbye.com"],
:bcc => ["test@example.com", "sam@bob.com", "goodbye@hello.com"],
:body => "Every email is spam."
end
end
@ -48,12 +50,54 @@ describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
matcher.failure_message.should =~ /Expected sent email with body/
end
it "accept sent e-mail based on the recipient" do
it "accepts sent e-mail based on the recipient" do
should have_sent_email.to('myself@me.com')
matcher = have_sent_email.to('you@example.com')
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email to/
end
it "accepts sent e-mail based on cc string" do
should have_sent_email.cc('joe@bob.com')
matcher = have_sent_email.cc('you@example.com')
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email cc/
end
it "accepts sent-email based on cc regex" do
should have_sent_email.cc(/@bob\.com/)
matcher = have_sent_email.cc(/us@/)
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email cc/
end
it "accepts sent e-mail based on cc list" do
should have_sent_email.with_cc(['you@you.com', 'joe@bob.com'])
matcher = have_sent_email.with_cc(['you@example.com'])
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email with cc/
end
it "accepts sent e-mail based on bcc string" do
should have_sent_email.bcc("goodbye@hello.com")
matcher = have_sent_email.bcc("test@hello.com")
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email bcc/
end
it "accepts sent e-mail based on bcc regex" do
should have_sent_email.bcc(/@example\.com/)
matcher = have_sent_email.bcc(/you@/)
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email bcc/
end
it "accepts sent e-mail based on bcc list" do
should have_sent_email.with_bcc(['sam@bob.com', 'test@example.com'])
matcher = have_sent_email.with_bcc(['you@you.com', 'joe@bob.com'])
matcher.matches?(nil)
matcher.failure_message.should =~ /Expected sent email with bcc/
end
it "lists all the deliveries within failure message" do
add_mail_to_deliveries