class Mail::Matchers::HasSentEmailMatcher

Public Class Methods

new(_context) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 9
def initialize(_context)
end

Public Instance Methods

bcc(recipient_or_list) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 44
def bcc(recipient_or_list)
  @blind_copy_recipients ||= []
  @blind_copy_recipients.concat(Array(recipient_or_list))
  self
end
cc(recipient_or_list) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 33
def cc(recipient_or_list)
  @copy_recipients ||= []

  if recipient_or_list.kind_of?(Array)
    @copy_recipients += recipient_or_list
  else
    @copy_recipients << recipient_or_list
  end
  self
end
description() click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 96
def description
  result = "send a matching email"
  result
end
failure_message() click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 101
def failure_message
  result = "Expected email to be sent "
  result += explain_expectations
  result += dump_deliveries
  result
end
failure_message_when_negated() click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 108
def failure_message_when_negated
  result = "Expected no email to be sent "
  result += explain_expectations
  result += dump_deliveries
  result
end
from(sender) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 17
def from(sender)
  @sender = sender
  self
end
matches?(subject) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 12
def matches?(subject)
  matching_deliveries = filter_matched_deliveries(Mail::TestMailer.deliveries)
  !(matching_deliveries.empty?)
end
matching_body(body_matcher) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 81
def matching_body(body_matcher)
  @body_matcher = body_matcher
  self
end
matching_subject(subject_matcher) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 71
def matching_subject(subject_matcher)
  @subject_matcher = subject_matcher
  self
end
to(recipient_or_list) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 22
def to(recipient_or_list)
  @recipients ||= []

  if recipient_or_list.kind_of?(Array)
    @recipients += recipient_or_list
  else
    @recipients << recipient_or_list
  end
  self
end
with_any_attachments() click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 61
def with_any_attachments
  @having_attachments = true
  self
end
with_attachments(attachments) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 50
def with_attachments(attachments)
  @attachments ||= []
  @attachments.concat(Array(attachments))
  self
end
with_body(body) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 76
def with_body(body)
  @body = body
  self
end
with_html(body) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 86
def with_html(body)
  @html_part_body = body
  self
end
with_no_attachments() click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 56
def with_no_attachments
  @having_attachments = false
  self
end
with_subject(subject) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 66
def with_subject(subject)
  @subject = subject
  self
end
with_text(body) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 91
def with_text(body)
  @text_part_body = body
  self
end

Protected Instance Methods

dump_deliveries() click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 196
def dump_deliveries
  "(actual deliveries: " + Mail::TestMailer.deliveries.inspect + ")"
end
explain_expectations() click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 181
def explain_expectations
  result = ''
  result += "from #{@sender} " if instance_variable_defined?('@sender')
  result += "to #{@recipients.inspect} " if instance_variable_defined?('@recipients')
  result += "cc #{@copy_recipients.inspect} " if instance_variable_defined?('@copy_recipients')
  result += "bcc #{@blind_copy_recipients.inspect} " if instance_variable_defined?('@blind_copy_recipients')
  result += "with subject \"#{@subject}\" " if instance_variable_defined?('@subject')
  result += "with subject matching \"#{@subject_matcher}\" " if instance_variable_defined?('@subject_matcher')
  result += "with body \"#{@body}\" " if instance_variable_defined?('@body')
  result += "with body matching \"#{@body_matcher}\" " if instance_variable_defined?('@body_matcher')
  result += "with a text part matching \"#{@text_part_body}\" " if instance_variable_defined?('@text_part_body')
  result += "with an HTML part matching \"#{@html_part_body}\" " if instance_variable_defined?('@html_part_body')
  result
end
filter_matched_deliveries(deliveries) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 117
def filter_matched_deliveries(deliveries)
  candidate_deliveries = deliveries
  modifiers =
    %w(sender recipients copy_recipients blind_copy_recipients subject
    subject_matcher body body_matcher html_part_body text_part_body  having_attachments attachments)
  modifiers.each do |modifier_name|
    next unless instance_variable_defined?("@#{modifier_name}")
    candidate_deliveries = candidate_deliveries.select{|matching_delivery| self.send("matches_on_#{modifier_name}?", matching_delivery)}
  end

  candidate_deliveries
end
matches_on_attachments?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 159
def matches_on_attachments?(delivery)
  @attachments.each_with_index.inject( true ) do |sent_attachments, (attachment, index)|
    sent_attachments &&= (attachment === delivery.attachments[index])
  end
end
matches_on_blind_copy_recipients?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 142
def matches_on_blind_copy_recipients?(delivery)
  @blind_copy_recipients.all? {|recipient| delivery.bcc.include?(recipient) }
end
matches_on_body?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 165
def matches_on_body?(delivery)
  delivery.body == @body
end
matches_on_body_matcher?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 169
def matches_on_body_matcher?(delivery)
  @body_matcher.match delivery.body.raw_source
end
matches_on_copy_recipients?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 138
def matches_on_copy_recipients?(delivery)
  @copy_recipients.all? {|recipient| delivery.cc.include?(recipient) }
end
matches_on_having_attachments?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 154
def matches_on_having_attachments?(delivery)
  @having_attachments && delivery.attachments.any? ||
    (!@having_attachments && delivery.attachments.none?)
end
matches_on_html_part_body?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 173
def matches_on_html_part_body?(delivery)
  delivery.html_part.body == @html_part_body
end
matches_on_recipients?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 134
def matches_on_recipients?(delivery)
  @recipients.all? {|recipient| delivery.to.include?(recipient) }
end
matches_on_sender?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 130
def matches_on_sender?(delivery)
  delivery.from.include?(@sender)
end
matches_on_subject?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 146
def matches_on_subject?(delivery)
  delivery.subject == @subject
end
matches_on_subject_matcher?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 150
def matches_on_subject_matcher?(delivery)
  @subject_matcher.match delivery.subject
end
matches_on_text_part_body?(delivery) click to toggle source
# File lib/mail/matchers/has_sent_mail.rb, line 177
def matches_on_text_part_body?(delivery)
  delivery.text_part.body == @text_part_body
end