Ruby and RSpec
# Gemfilegroup :test do gem "mailcatchr"endA helper
Section titled “A helper”module Mail CLIENT = Mailcatchr::Client.new(api_key: ENV.fetch("MAILCATCHR_API_KEY")) MAILBOX_ID = ENV.fetch("MAILCATCHR_MAILBOX_ID")
# A fresh address per example so parallel workers never see each other's mail. def unique_address(label) "#{label}-#{(Time.now.to_f * 1000).to_i}@<mailbox-id>.mailcatchr.com" end
def wait_for_mail(to:, subject: nil) summary = CLIENT.messages.wait_for(MAILBOX_ID, to: to, subject: subject, timeout_seconds: 30) raise "No email to #{to} within 30 s" if summary.nil?
CLIENT.messages.get(MAILBOX_ID, summary.id) endend
RSpec.configure { |c| c.include Mail }The example
Section titled “The example”RSpec.describe "password reset", type: :system do it "sends a link that works" do address = unique_address("reset") visit "/password/new" fill_in "Email", with: address click_button "Send reset link"
message = wait_for_mail(to: address, subject: "Reset your password") link = message.text_body[%r{https://\S+/reset\?token=\S+}] expect(link).to be_present
visit link expect(page).to have_content("Choose a new password") endendTwo-factor codes
Section titled “Two-factor codes”fill_in "otp", with: Mail::CLIENT.totps.code(ENV.fetch("MAILCATCHR_TOTP_ID")).codeRails and ActionMailer
Section titled “Rails and ActionMailer”Leave delivery configured exactly as in production. The test users’ addresses are on your mailbox subdomain, so the mail is delivered to mailcatchr through your provider. If you prefer to bypass the provider, point config.action_mailer.smtp_settings at smtp.mailcatchr.com on port 587 in the test environment.