Add bcc and cc functionality to the HaveSentEmailMatcher
This commit is contained in:
parent
224f2e5a70
commit
4e199e085e
|
@ -41,12 +41,36 @@ module Shoulda # :nodoc:
|
||||||
self
|
self
|
||||||
end
|
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)
|
def matches?(subject)
|
||||||
::ActionMailer::Base.deliveries.each do |mail|
|
::ActionMailer::Base.deliveries.each do |mail|
|
||||||
@subject_failed = !regexp_or_string_match(mail.subject, @email_subject) if @email_subject
|
@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 = !regexp_or_string_match(mail.body, @body) if @body
|
||||||
@sender_failed = !regexp_or_string_match_in_array(mail.from, @sender) if @sender
|
@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
|
@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?
|
return true unless anything_failed?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -67,6 +91,10 @@ module Shoulda # :nodoc:
|
||||||
description << " containing #{@body.inspect}" if @body
|
description << " containing #{@body.inspect}" if @body
|
||||||
description << " from #{@sender.inspect}" if @sender
|
description << " from #{@sender.inspect}" if @sender
|
||||||
description << " to #{@recipient.inspect}" if @recipient
|
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
|
description
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,6 +106,10 @@ module Shoulda # :nodoc:
|
||||||
expectation << " with body #{@body.inspect}" if @body_failed
|
expectation << " with body #{@body.inspect}" if @body_failed
|
||||||
expectation << " from #{@sender.inspect}" if @sender_failed
|
expectation << " from #{@sender.inspect}" if @sender_failed
|
||||||
expectation << " to #{@recipient.inspect}" if @recipient_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}"
|
expectation << "\nDeliveries:\n#{inspect_deliveries}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -88,7 +120,7 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def anything_failed?
|
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
|
end
|
||||||
|
|
||||||
def regexp_or_string_match(a_string, a_regexp_or_string)
|
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)
|
an_array.include?(a_regexp_or_string)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,6 +12,8 @@ describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
|
||||||
mail :from => "do-not-reply@example.com",
|
mail :from => "do-not-reply@example.com",
|
||||||
:to => "myself@me.com",
|
:to => "myself@me.com",
|
||||||
:subject => "This is spam",
|
: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."
|
:body => "Every email is spam."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -48,13 +50,55 @@ describe Shoulda::Matchers::ActionMailer::HaveSentEmailMatcher do
|
||||||
matcher.failure_message.should =~ /Expected sent email with body/
|
matcher.failure_message.should =~ /Expected sent email with body/
|
||||||
end
|
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')
|
should have_sent_email.to('myself@me.com')
|
||||||
matcher = have_sent_email.to('you@example.com')
|
matcher = have_sent_email.to('you@example.com')
|
||||||
matcher.matches?(nil)
|
matcher.matches?(nil)
|
||||||
matcher.failure_message.should =~ /Expected sent email to/
|
matcher.failure_message.should =~ /Expected sent email to/
|
||||||
end
|
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
|
it "lists all the deliveries within failure message" do
|
||||||
add_mail_to_deliveries
|
add_mail_to_deliveries
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue