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
- #clear_emails ⇒ Object
- #email_body(email) ⇒ Object
- #emails ⇒ Object
- #last_email ⇒ Object
- #last_email_body ⇒ Object
- #last_email_first_link ⇒ Object
- #last_email_link ⇒ Object
- #wait_for_email(options = {}) ⇒ Object
Instance Method Details
#clear_emails ⇒ Object
16 17 18 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 16 def clear_emails ActionMailer::Base.deliveries.clear end |
#email_body(email) ⇒ Object
28 29 30 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 28 def email_body(email) (email.try(:html_part).try(:body) || email.body).encoded end |
#emails ⇒ Object
12 13 14 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 12 def emails ActionMailer::Base.deliveries end |
#last_email ⇒ Object
20 21 22 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 20 def last_email emails.last end |
#last_email_body ⇒ Object
24 25 26 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 24 def last_email_body email_body(last_email) end |
#last_email_first_link ⇒ Object
36 37 38 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 36 def last_email_first_link Nokogiri::HTML(last_email_body).css("table.content a").first["href"] end |
#last_email_link ⇒ Object
32 33 34 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 32 def last_email_link Nokogiri::HTML(last_email_body).css("table.content a").last["href"] end |
#wait_for_email(options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/decidim/dev/test/rspec_support/action_mailer.rb', line 40 def wait_for_email( = {}) [:max_attempts] ||= 3 attempts = 0 loop do if attempts >= [:max_attempts] raise StandardError, "An email with subject containing '#{options[:subject]}' wasn't sent.'" end return if last_email&.subject&.include? [:subject] sleep 1 attempts += 1 end end |