thoughtbot / suspenders

A Rails template with our standard defaults.
https://thoughtbot.com
MIT License
4.01k stars 530 forks source link

Add email helper methods to spec helper #1125

Open stevepolitodesign opened 1 year ago

stevepolitodesign commented 1 year ago

When writing integration and/or system tests, I want to be able to easily test mailer links so that my developer experience is improved.

Acceptance Criteria

stevepolitodesign commented 1 year ago

I think it would be a nice addition if we added current_email and email_link helper methods for testing. Below is are the proposed helper methods inspired from a personal post. Similar to capybara-email

def open_latest_email_for(email_address)
  @current_email = ActionMailer::Base.deliveries.reverse.detect do |email|
    email.to.include?(email_address)
  end
end

def current_email
  @current_email ||= ActionMailer::Base.deliveries.last
end

def email_link(email, string)
  document = Capybara.string(email.body.to_s)
  link = document.find(:link, string)[:href]

  localize_link(link)
end

private

def localize_link(link)
  uri = URI.parse(link)

  if uri.query
    "#{uri.path}?#{uri.query}"
  else
    uri.path
  end
end