class RSpec::Rails::Matchers::SendEmail

@api private

Matcher class for `send_email`. Should not be instantiated directly.

@see RSpec::Rails::Matchers#send_email

Constants

INSPECT_EMAIL_ATTRIBUTES

@api private Define the email attributes that should be included in the inspection output.

Public Class Methods

new(criteria) click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 16
def initialize(criteria)
  @criteria = criteria
end

Public Instance Methods

failure_message() click to toggle source

@api private @return [String]

# File lib/rspec/rails/matchers/send_email.rb, line 38
def failure_message
  result =
    if multiple_match?
      "More than 1 matching emails were sent."
    else
      "No matching emails were sent."
    end
  "#{result}#{sent_emails_message}"
end
failure_message_when_negated() click to toggle source

@api private @return [String]

# File lib/rspec/rails/matchers/send_email.rb, line 50
def failure_message_when_negated
  "Expected not to send an email but it was sent."
end
matches?(block) click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 30
def matches?(block)
  define_matched_emails(block)

  @matched_emails.one?
end
supports_block_expectations?() click to toggle source

@api private

# File lib/rspec/rails/matchers/send_email.rb, line 26
def supports_block_expectations?
  true
end
supports_value_expectations?() click to toggle source

@api private

# File lib/rspec/rails/matchers/send_email.rb, line 21
def supports_value_expectations?
  false
end

Private Instance Methods

define_matched_emails(block) click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 64
def define_matched_emails(block)
  before = deliveries.dup

  block.call

  after = deliveries

  @diff = after - before
  @matched_emails = @diff.select(&method(:matched_email?))
end
deliveries() click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 60
def deliveries
  ActionMailer::Base.deliveries
end
diffable?() click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 56
def diffable?
  true
end
matched_email?(email) click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 75
def matched_email?(email)
  @criteria.all? do |attr, value|
    expected =
      case attr
      when :to, :from, :cc, :bcc then Array(value)
      else
        value
      end

    values_match?(expected, email.public_send(attr))
  end
end
multiple_match?() click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 88
def multiple_match?
  @matched_emails.many?
end
sent_emails_message() click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 92
def sent_emails_message
  if @diff.empty?
    "\n\nThere were no any emails sent inside the expectation block."
  else
    sent_emails =
      @diff.map do |email|
        inspected = INSPECT_EMAIL_ATTRIBUTES.map { |attr| "#{attr}: #{email.public_send(attr)}" }.join(", ")
        "- #{inspected}"
      end.join("\n")
    "\n\nThe following emails were sent:\n#{sent_emails}"
  end
end