简体   繁体   中英

Content of bounced emails in tests for Action Mailbox

How can I check the content of a bounced email?

require "rails_helper"

RSpec.describe EventsMailbox do
  it "requires a current user" do
    expect(mail_processed).to have_bounced
    # here I would like to check the content of the bounced email
  end

  def mail
    Mail.new(
      from: "dorian@dorianmarie.fr",
      subject: "Some subject",
      body: "Some body"
    )
  end

  def mail_processed
    @mail_processed ||= process(mail)
  end
end

Here is how I did it, the key was that perform_enqueued_jobs that allows emails to be retrieved from ActionMailer::Base.deliveries :

require "rails_helper"

RSpec.describe EventsMailbox do
  include ActiveJob::TestHelper

  around(:each) { |example| perform_enqueued_jobs { example.run } }

  it "requires a current user", focus: true do
    expect(mail_processed).to have_bounced
    expect(last_delivery.from).to eq(["contact@socializus.app"])
    expect(last_delivery.to).to eq(["dorian@dorianmarie.fr"])
    expect(last_delivery_text).to(
      include("You need to be registered on Socializus")
    )
  end

  def mail
    Mail.new(
      from: "dorian@dorianmarie.fr",
      to: "party@events.socializus.app",
      subject: "Some subject",
      body: "Some body"
    )
  end

  def mail_processed
    @mail_processed ||= process(mail)
  end

  def last_delivery
    ActionMailer::Base.deliveries.last
  end

  def last_delivery_text
    return unless last_delivery
    text_part = last_delivery.parts.detect { _1.mime_type == "text/plain" }
    return unless text_part
    text_part.body.to_s
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM