稍后交付在 Rails 5 中的测试环境中不工作

Deliver later not working in the test environment in Rails 5

基本上,通过测试配置设置它在 Rails 4 中的工作方式(传递方法设置为测试等),除了我已替换的已弃用选项外,邮件仅发送 deliver_now,而不是 deliver_later。 Deliver_later 在开发环境中工作,即使两个环境之间的配置相同。

测试环境邮件程序配置:

config.action_mailer.delivery_method = :test
config.action_mailer.perform_deliveries = true
config.action_mailer.perform_caching = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }

我有同样的问题,我转而使用断言

enqueued_jobs.size

断言我的邮件已排队。此外,我对邮件 class 进行了单元测试,我使用 deliver_now.

这需要包括 ActiveJob:TestHelper

class ActiveSupport::TestCase
  include ActiveJob::TestHelper
end

测试使用 deliver_later(活动作业)的电子邮件的另一个选项是将您现在要执行的代码(未排队)和您的断言放在 perform_enqueued_jobs 块中。这还需要在 class 定义

下方的测试文件顶部添加一个包含
include ActiveJob::TestHelper

然后像下面这样。

perform_enqueued_jobs do
  post article_url, params: { article: { title: "Learn Testing", body: "Lorem Ipsum" } }
  assert_not_equal 0, ActionMailer::Base.deliveries.size
end