Action 邮箱规格

邮箱规格提供了 `ActiveMailbox::TestHelper` 中可用的替代断言,并帮助断言电子邮件如何路由、传递、弹回或失败的行为。

邮箱规格用 `type: :mailbox` 标记,或者如果您已将 `config.infer_spec_type_from_file_location!` 设置为将它们放在 `spec/mailboxes` 中。

使用邮箱规格,您可以:* `process(mail_or_attributes)` - 将邮件直接发送到正在测试的邮箱进行 `process`ing。* `receive_inbound_email(mail_or_attributes)` - 匹配器,用于断言传入的电子邮件是否会路由到正在测试的邮箱。* `have_been_delivered` - 匹配器,用于断言传入的电子邮件对象是否已传递。* `have_bounced` - 匹配器,用于断言传入的电子邮件对象是否已弹回。* `have_failed` - 匹配器,用于断言传入的电子邮件对象是否已失败。

背景

给定 action 邮箱可用。

简单测试邮件是否正确路由

给定 一个名为“app/mailboxes/application_mailbox.rb”的文件,内容为

class ApplicationMailbox < ActionMailbox::Base
  routing (/^replies@/i) => :inbox
end

以及 一个名为“app/maiboxes/inbox_mailbox.rb”的文件,内容为

class InboxMailbox < ApplicationMailbox
  def process
    case mail.subject
    when (/^\[\d*\]/i)
      # ok
    when (/^\[\w*\]/i)
      bounced!
    else
      raise "Invalid email subject"
    end
  end
end

以及 一个名为“spec/mailboxes/inbox_mailbox_spec.rb”的文件,内容为

require 'rails_helper'

RSpec.describe InboxMailbox, type: :mailbox do
  it "route email to properly mailbox" do
    expect(InboxMailbox)
      .to receive_inbound_email(to: "replies@example.com")
  end

  it "marks email as delivered when number tag in subject is valid" do
    mail = Mail.new(
      from: "replies@example.com",
      subject: "[141982763] support ticket"
    )
    mail_processed = process(mail)

    expect(mail_processed).to have_been_delivered
  end

  it "marks email as bounced when number tag in subject is invalid" do
    mail = Mail.new(
      from: "replies@example.com",
      subject: "[111X] support ticket"
    )
    mail_processed = process(mail)

    expect(mail_processed).to have_bounced
  end

  it "marks email as failed when subject is invalid" do
    mail = Mail.new(
      from: "replies@example.com",
      subject: "INVALID"
    )

    expect {
      expect(process(mail)).to have_failed
    }.to raise_error(/Invalid email subject/)
  end
end

我运行 `rspec spec/mailboxes/inbox_mailbox_spec.rb`

那么 示例应该通过。