Sha256: 9801d60eff70d0e03191f6bcde9f248c3d8a183fee25b133a90615a61e9ce500

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Seatbelt
  module AssertMail

    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

    def assert_no_mail(options={}, &block)
      assert find_email(options, &block).nil?, "didn't expect mail (#{options.inspect}) in #{ActionMailer::Base.deliveries.inspect}"
    end

  private

    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

    def got_mail?(mail, options={})
      return false if options[:to]      && !mail.to.include?(options[:to])
      return false if options[:from]    && !mail.from.include?(options[:from])
      return false if options[:subject] && (mail.subject !~ /#{options[:subject]}/)
      return false if options[:cc]      && !mail.cc.include?(options[:cc])
      return false if options[:bcc]     && !mail.bcc.include?(options[:bcc])
      if options[:body]
        Array(options[:body]).each do |element|
          if !mail.body.encoded.match(element)
            # puts "#{element} not found in body: #{mail.body.encoded}"
            return false
          end
        end
      end
      true
    end

  end
end

if Module.const_defined?(:ActionMailer)
  class Test::Unit::TestCase
    include Seatbelt::AssertMail
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
seatbelt-0.1.0 lib/seatbelt/assert_mail.rb