module Seatbelt::AssertMail

Public Instance Methods

assert_mail(options={}, &block) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 4
def assert_mail(options={}, &block)
  assert !find_email(options, &block).nil?, "couldn't find the expected mail (#{options.inspect}) in #{ActionMailer::Base.deliveries.inspect}"
end
assert_no_mail(options={}, &block) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 8
def assert_no_mail(options={}, &block)
  assert find_email(options, &block).nil?, "didn't expect mail (#{options.inspect}) in #{ActionMailer::Base.deliveries.inspect}"
end
Also aliased as: refute_mail
refute_mail(options={}, &block)
Alias for: assert_no_mail

Private Instance Methods

find_email(options) { || ... } click to toggle source
# File lib/seatbelt/assert_mail.rb, line 15
def find_email(options, &block)
  if block_given?
    ActionMailer::Base.deliveries.clear
    yield
  end
  ActionMailer::Base.deliveries.detect do |mail|
    got_mail?(mail, options)
  end
end
got_mail?(mail, options={}) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 25
def got_mail?(mail, options={})
  return false if unexpected_recipient?(mail, options)
  return false if unexpected_sender?(mail, options)
  return false if unexpected_subject?(mail, options)
  return false if unexpected_copy?(mail, options)
  return false if unexpected_blind_copy?(mail, options)
  return false if unexpected_body?(mail, options)
  true
end
unexpected_blind_copy?(mail, options) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 58
def unexpected_blind_copy?(mail, options)
  options[:bcc] && !mail.bcc.include?(options[:bcc])
end
unexpected_body?(mail, options) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 62
def unexpected_body?(mail, options)
  if options[:body]
    Array(options[:body]).each do |element|
      if !mail.body.match(element)
        # puts "#{element} not found in body: #{mail.body}"
        return true
      end
    end
  end
  false
end
unexpected_copy?(mail, options) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 54
def unexpected_copy?(mail, options)
  options[:cc] && !mail.cc.include?(options[:cc])
end
unexpected_recipient?(mail, options) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 35
def unexpected_recipient?(mail, options)
  options[:to] && !mail.to.include?(options[:to])
end
unexpected_sender?(mail, options) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 39
def unexpected_sender?(mail, options)
  options[:from] && !mail.from.include?(options[:from])
end
unexpected_subject?(mail, options) click to toggle source
# File lib/seatbelt/assert_mail.rb, line 43
def unexpected_subject?(mail, options)
  case options[:subject]
  when String
    mail.subject != options[:subject]
  when Regexp
    mail.subject !~ /#{options[:subject]}/
  else
    false
  end
end