module EmailTestHelpers

Constants

NotFound
VERSION

Public Instance Methods

emails() click to toggle source
# File lib/email_test_helpers.rb, line 12
def emails
  ActionMailer::Base.deliveries
end
find_email(options = {}) click to toggle source
# File lib/email_test_helpers.rb, line 16
def find_email(options = {})
  validate_options(options)
  @last_email = emails.reverse.detect do |mail|
    [
      options[:to].nil?      || mail.to.include?(options[:to]),
      options[:cc].nil?      || mail.cc.include?(options[:cc]),
      options[:bcc].nil?     || mail.bcc.include?(options[:bcc]),
      options[:subject].nil? || options[:subject] === mail.subject,
      options[:body].nil?    || options[:body] === email_body(mail),
    ].all?
  end or raise(NotFound, "Couldn't find email with options: #{options.inspect}")
end
reset_last_email() click to toggle source
# File lib/email_test_helpers.rb, line 49
def reset_last_email
  @last_email = nil
end

Private Instance Methods

email_body(mail = last_email) click to toggle source
# File lib/email_test_helpers.rb, line 55
def email_body(mail = last_email)
  if multipart?
    mail.body.parts.select do |part|
      part.content_type.starts_with?('text/')
    end.map(&:body).map(&:raw_source).join
  else
    mail.body.raw_source
  end
end
last_email() click to toggle source
# File lib/email_test_helpers.rb, line 65
def last_email
  @last_email || find_email
end
multipart?(mail = last_email) click to toggle source
# File lib/email_test_helpers.rb, line 69
def multipart?(mail = last_email)
  mail.parts.any?
end
validate_options(options) click to toggle source
# File lib/email_test_helpers.rb, line 73
def validate_options(options)
  valid_keys = [:to, :cc, :bcc, :subject, :body]
  invalid_keys = options.keys - valid_keys
  if invalid_keys.any?
    raise ArgumentError, "Invalid options detected: #{invalid_keys.join(', ')}"
  end
end