Sha256: 932c65a46717717708552ff4bcf5c09e7d0fb132e4a9fd94d049353a20c392a1

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module EmailSpec
  module Deliveries
    def all_emails
      deliveries
    end

    def last_email_sent
      deliveries.last || raise("No email has been sent!")
    end

    def reset_mailer
      if defined?(ActionMailer) && ActionMailer::Base.delivery_method == :activerecord
        Email.delete_all
      elsif defined?(ActionMailer) && ActionMailer::Base.delivery_method == :cache
        mailer.clear_cache
      else
        deliveries.clear
      end
    end

    def mailbox_for(address)
      deliveries.select { |email| email.destinations.include?(address) }
    end

    protected

    def deliveries
      if defined?(Pony)
        Pony.deliveries
      elsif ActionMailer::Base.delivery_method == :activerecord
        Email.all.map { |email| parse_ar_to_mail(email) }
      elsif ActionMailer::Base.delivery_method == :cache
        mailer.cached_deliveries
      else
        mailer.deliveries
      end
    end

    def mailer
      ActionMailer::Base
    end

    def parse_ar_to_mail(email)
      Mail.read(email.mail)
    end
  end

  if defined?(Pony)
    module ::Pony
      def self.deliveries
        @deliveries ||= []
      end

      def self.mail(options)
        deliveries << build_mail(options)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
email_spec-2.3.0 lib/email_spec/deliveries.rb
email_spec-2.2.2 lib/email_spec/deliveries.rb