module EmailTestHelpers
Constants
- NotFound
- VERSION
Public Instance Methods
click_email_link(key = nil)
click to toggle source
# File lib/email_test_helpers.rb, line 8 def click_email_link(key = nil) visit find_email_link(key) end
emails()
click to toggle source
# File lib/email_test_helpers.rb, line 12 def emails ActionMailer::Base.deliveries end
find_email(options = {})
click to toggle source
# File lib/email_test_helpers.rb, line 16 def find_email(options = {}) validate_options(options) @last_email = emails.reverse.detect do |mail| [ options[:to].nil? || mail.to.include?(options[:to]), options[:cc].nil? || mail.cc.include?(options[:cc]), options[:bcc].nil? || mail.bcc.include?(options[:bcc]), options[:subject].nil? || options[:subject] === mail.subject, options[:body].nil? || options[:body] === email_body(mail), ].all? end or raise(NotFound, "Couldn't find email with options: #{options.inspect}") end
find_email_link(key = nil)
click to toggle source
# File lib/email_test_helpers.rb, line 29 def find_email_link(key = nil) if key link = Capybara.string(email_body).all('a').detect do |element| case key when Regexp then key === element.text || key === element[:href] when String then element.text.downcase.include?(key.downcase) || element[:href].include?(key) else raise TypeError, "key must be Regexp or String (was #{key.class.name})" end end if link link[:href] else raise(NotFound, "Couldn't find link with key: #{key}") end else link = Capybara.string(email_body).first('a') or raise(NotFound, "Couldn't find any links") link[:href] end end
reset_last_email()
click to toggle source
# File lib/email_test_helpers.rb, line 49 def reset_last_email @last_email = nil end
Private Instance Methods
email_body(mail = last_email)
click to toggle source
# File lib/email_test_helpers.rb, line 55 def email_body(mail = last_email) if multipart? mail.body.parts.select do |part| part.content_type.starts_with?('text/') end.map(&:body).map(&:raw_source).join else mail.body.raw_source end end
last_email()
click to toggle source
# File lib/email_test_helpers.rb, line 65 def last_email @last_email || find_email end
multipart?(mail = last_email)
click to toggle source
# File lib/email_test_helpers.rb, line 69 def multipart?(mail = last_email) mail.parts.any? end
validate_options(options)
click to toggle source
# File lib/email_test_helpers.rb, line 73 def validate_options(options) valid_keys = [:to, :cc, :bcc, :subject, :body] invalid_keys = options.keys - valid_keys if invalid_keys.any? raise ArgumentError, "Invalid options detected: #{invalid_keys.join(', ')}" end end