为什么我不能在 Rails 5 中测试电子邮件发送?

Why can't I test email sending in Rails 5?

我正在尝试使用 cucumber and using the ActionMailer guide and Testing guide 在一个简单的 Rails 5 应用程序中测试发送电子邮件的简单情况。你能帮我看看为什么它不起作用吗?

app/mailers/test_mailer.rb

class TestMailer < ApplicationMailer
  def welcome_email
    @greeting = "Hi"
    mail to: "to@example.org", subject: 'welcome!'
  end
end

features/test.特征

Feature: test email

  Background:
    Given we say "hello"

  Scenario: send mail
    Given 0 email is in the queue
     Then send an email
    Given 1 email is in the queue

features/steps/test_steps.rb

Given "we say {string}"  do |say_it|
  puts say_it
end

Given "{int} email is in the queue"  do |mail_count|
  puts "method    : #{ActionMailer::Base.delivery_method}"
  puts "deliveries: #{ActionMailer::Base.perform_deliveries}"
  ActionMailer::Base.deliveries.count.should eq mail_count
end

Then "send an email"  do
  TestMailer.welcome_email.deliver_later
end

一直响应队列中没有项目。

不要deliver_later,现在就送。如果您 deliver_later 您必须 运行 您的后台作业,然后您的邮件才会被添加到队列中

正如我对@diabolist 的回应,我不得不修改我的黄瓜测试设置以支持 :async 而不是 :inline。这需要:

config/environments/test.rb

...
config.active_job.queue.adapter = :test
...

features/support/env.rb

...
World( ActiveJob::TestHelper )
Around() do |scenario, block|
  perform_enqueued_jobs do
    block.call
  end
end

我意识到我可能只是将我的测试适配器切换到 :inline,但这将让我稍后进行一些队列测试 — 特别是使用 performed 方法。