Module: MailerHelpers

Defined in:
lib/decidim/dev/test/rspec_support/action_mailer.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#clear_emailsObject



17
18
19
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 17

def clear_emails
  ActionMailer::Base.deliveries.clear
end

#email_body(email) ⇒ Object



29
30
31
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 29

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

#emailsObject



13
14
15
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 13

def emails
  ActionMailer::Base.deliveries
end

#last_emailObject



21
22
23
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 21

def last_email
  emails.last
end

#last_email_bodyObject



25
26
27
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 25

def last_email_body
  email_body(last_email)
end


37
38
39
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 37

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


33
34
35
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 33

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

#wait_for_email(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 41

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