2010-12-15 22:34:19 +00:00
|
|
|
module Shoulda # :nodoc:
|
|
|
|
module Matchers
|
|
|
|
module ActionMailer # :nodoc:
|
|
|
|
|
|
|
|
# The right email is sent.
|
|
|
|
#
|
|
|
|
# it { should have_sent_email.with_subject(/is spam$/) }
|
|
|
|
# it { should have_sent_email.from('do-not-reply@example.com') }
|
|
|
|
# it { should have_sent_email.with_body(/is spam\./) }
|
|
|
|
# it { should have_sent_email.to('myself@me.com') }
|
|
|
|
# it { should have_sent_email.with_subject(/spam/).
|
|
|
|
# from('do-not-reply@example.com').
|
|
|
|
# with_body(/spam/).
|
|
|
|
# to('myself@me.com') }
|
2011-05-27 14:50:23 +00:00
|
|
|
#
|
|
|
|
# Use values of instance variables
|
|
|
|
# it {should have_sent_email.to {@user.email} }
|
2010-12-15 22:34:19 +00:00
|
|
|
def have_sent_email
|
2011-05-27 14:50:23 +00:00
|
|
|
HaveSentEmailMatcher.new(self)
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class HaveSentEmailMatcher # :nodoc:
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def initialize(context)
|
|
|
|
@context = context
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_context(context)
|
|
|
|
@context = context
|
|
|
|
self
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def with_subject(email_subject = nil, &block)
|
2010-12-15 22:34:19 +00:00
|
|
|
@email_subject = email_subject
|
2011-05-27 14:50:23 +00:00
|
|
|
@email_subject_block = block
|
2010-12-15 22:34:19 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def from(sender = nil, &block)
|
2010-12-15 22:34:19 +00:00
|
|
|
@sender = sender
|
2011-05-27 14:50:23 +00:00
|
|
|
@sender_block = block
|
2010-12-15 22:34:19 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def with_body(body = nil, &block)
|
2010-12-15 22:34:19 +00:00
|
|
|
@body = body
|
2011-05-27 14:50:23 +00:00
|
|
|
@body_block = block
|
2010-12-15 22:34:19 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def to(recipient = nil, &block)
|
2010-12-15 22:34:19 +00:00
|
|
|
@recipient = recipient
|
2011-05-27 14:50:23 +00:00
|
|
|
@recipient_block = block
|
2010-12-15 22:34:19 +00:00
|
|
|
self
|
|
|
|
end
|
2011-05-27 13:49:25 +00:00
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def cc(recipient = nil, &block)
|
2011-05-27 13:49:25 +00:00
|
|
|
@cc = recipient
|
2011-05-27 14:50:23 +00:00
|
|
|
@cc_block = block
|
2011-05-27 13:49:25 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def with_cc(recipients = nil, &block)
|
2011-05-27 13:49:25 +00:00
|
|
|
@cc_recipients = recipients
|
2011-05-27 14:50:23 +00:00
|
|
|
@cc_recipients_block = block
|
2011-05-27 13:49:25 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def bcc(recipient = nil, &block)
|
2011-05-27 13:49:25 +00:00
|
|
|
@bcc = recipient
|
2011-05-27 14:50:23 +00:00
|
|
|
@bcc_block = block
|
2011-05-27 13:49:25 +00:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def with_bcc(recipients = nil, &block)
|
2011-05-27 13:49:25 +00:00
|
|
|
@bcc_recipients = recipients
|
2011-05-27 14:50:23 +00:00
|
|
|
@bcc_recipients_block = block
|
2011-05-27 13:49:25 +00:00
|
|
|
self
|
|
|
|
end
|
2010-12-15 22:34:19 +00:00
|
|
|
|
|
|
|
def matches?(subject)
|
2011-05-27 14:50:23 +00:00
|
|
|
normalize_blocks
|
2010-12-15 22:34:19 +00:00
|
|
|
::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
|
|
|
|
@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
|
2011-05-27 13:49:25 +00:00
|
|
|
@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
|
2010-12-15 22:34:19 +00:00
|
|
|
return true unless anything_failed?
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def failure_message
|
|
|
|
"Expected #{expectation}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def negative_failure_message
|
|
|
|
"Did not expect #{expectation}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2010-12-17 16:50:49 +00:00
|
|
|
description = "send an email"
|
|
|
|
description << " with a subject of #{@email_subject.inspect}" if @email_subject
|
|
|
|
description << " containing #{@body.inspect}" if @body
|
|
|
|
description << " from #{@sender.inspect}" if @sender
|
|
|
|
description << " to #{@recipient.inspect}" if @recipient
|
2011-05-27 13:49:25 +00:00
|
|
|
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
|
2010-12-17 16:50:49 +00:00
|
|
|
description
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def expectation
|
|
|
|
expectation = "sent email"
|
|
|
|
expectation << " with subject #{@email_subject.inspect}" if @subject_failed
|
|
|
|
expectation << " with body #{@body.inspect}" if @body_failed
|
|
|
|
expectation << " from #{@sender.inspect}" if @sender_failed
|
|
|
|
expectation << " to #{@recipient.inspect}" if @recipient_failed
|
2011-05-27 13:49:25 +00:00
|
|
|
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
|
2010-12-15 22:34:19 +00:00
|
|
|
expectation << "\nDeliveries:\n#{inspect_deliveries}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect_deliveries
|
|
|
|
::ActionMailer::Base.deliveries.map do |delivery|
|
|
|
|
"#{delivery.subject.inspect} to #{delivery.to.inspect}"
|
|
|
|
end.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def anything_failed?
|
2011-05-27 13:49:25 +00:00
|
|
|
@subject_failed || @body_failed || @sender_failed || @recipient_failed || @cc_failed || @cc_recipients_failed || @bcc_failed || @bcc_recipients_failed
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 14:50:23 +00:00
|
|
|
def normalize_blocks
|
|
|
|
@email_subject = @context.instance_eval(&@email_subject_block) if @email_subject_block
|
|
|
|
@sender = @context.instance_eval(&@sender_block) if @sender_block
|
|
|
|
@body = @context.instance_eval(&@body_block) if @body_block
|
|
|
|
@recipient = @context.instance_eval(&@recipient_block) if @recipient_block
|
|
|
|
@cc = @context.instance_eval(&@cc_block) if @cc_block
|
|
|
|
@cc_recipients = @context.instance_eval(&@cc_recipients_block) if @cc_recipients_block
|
|
|
|
@bcc = @context.instance_eval(&@bcc_block) if @bcc_block
|
|
|
|
@bcc_recipients = @context.instance_eval(&@bcc_recipients_block) if @bcc_recipients_block
|
|
|
|
end
|
|
|
|
|
2010-12-15 22:34:19 +00:00
|
|
|
def regexp_or_string_match(a_string, a_regexp_or_string)
|
|
|
|
case a_regexp_or_string
|
|
|
|
when Regexp
|
|
|
|
a_string =~ a_regexp_or_string
|
|
|
|
when String
|
|
|
|
a_string == a_regexp_or_string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def regexp_or_string_match_in_array(an_array, a_regexp_or_string)
|
|
|
|
case a_regexp_or_string
|
|
|
|
when Regexp
|
|
|
|
an_array.any? { |string| string =~ a_regexp_or_string }
|
|
|
|
when String
|
|
|
|
an_array.include?(a_regexp_or_string)
|
|
|
|
end
|
|
|
|
end
|
2011-05-27 13:49:25 +00:00
|
|
|
|
|
|
|
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
|
2010-12-15 22:34:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|