Sha256: 20b458308d9a6c0ae72c1792b92521b6d8432fa98c85f4a9fd5a4dc495e1d439

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require "nokogiri"

RSpec.configure do |config|
  config.before(:each) { clear_emails }
end

# A set of helpers meant to make your life easier when testing
# emails, especially given the fact that ActionMailer's API can
# be a bit inconsistent.
module MailerHelpers
  def emails
    ActionMailer::Base.deliveries
  end

  def clear_emails
    ActionMailer::Base.deliveries.clear
  end

  def last_email
    emails.last
  end

  def last_email_body
    email_body(last_email)
  end

  def email_body(email)
    (email.try(:html_part).try(:body) || email.body).encoded
  end

  def last_email_link
    Nokogiri::HTML(last_email_body).css("table.content a").last["href"]
  end

  def last_email_first_link
    Nokogiri::HTML(last_email_body).css("table.content a").first["href"]
  end

  def wait_for_email(options = {})
    options[:max_attempts] ||= 3
    attempts = 0
    loop do
      if attempts >= options[:max_attempts]
        raise StandardError, "An email with subject containing '#{options[:subject]}' wasn't sent.'"
      end

      return if last_email&.subject&.include? options[:subject]

      sleep 1
      attempts += 1
    end
  end
end

RSpec.configure do |config|
  config.include MailerHelpers
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-dev-0.8.4 lib/decidim/dev/test/rspec_support/action_mailer.rb
decidim-dev-0.8.3 lib/decidim/dev/test/rspec_support/action_mailer.rb
decidim-dev-0.8.2 lib/decidim/dev/test/rspec_support/action_mailer.rb
decidim-dev-0.8.1 lib/decidim/dev/test/rspec_support/action_mailer.rb
decidim-dev-0.8.0 lib/decidim/dev/test/rspec_support/action_mailer.rb