class RSpec::Rails::Matchers::SendEmail
@api private
Matcher class for `send_email`. Should not be instantiated directly.
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 15 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 37 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 49 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 29 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 25 def supports_block_expectations? true end
supports_value_expectations?()
click to toggle source
@api private
# File lib/rspec/rails/matchers/send_email.rb, line 20 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 63 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 59 def deliveries ActionMailer::Base.deliveries end
diffable?()
click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 55 def diffable? true end
matched_email?(email)
click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 74 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 87 def multiple_match? @matched_emails.many? end
sent_emails_message()
click to toggle source
# File lib/rspec/rails/matchers/send_email.rb, line 91 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