测试排队的电子邮件数量?

Jar*_*eck 1 ruby rspec ruby-on-rails rspec-rails rails-activejob

我们可以检查enqueued_jobs.length,前提是电子邮件是唯一可能的后台作业类型。

it 'sends exactly one, particular email' do
  expect { post :create }.to(
    # First, we can check about the particular email we care about.
    have_enqueued_mail(MyMailer, :my_particular_email)
  )

  # But we also have to check that no other email was sent.
  expect(ActiveJob::Base.queue_adapter.enqueued_jobs.length).to eq(1)
end
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来断言:

  1. MyMailer.my_particular_email已排队,
  2. 没有其他电子邮件排队,
  3. 我们不关心其他非电子邮件后台作业是否已排队

Sch*_*ern 5

我相信once会与这个匹配器一起工作。

expect {
  post :create
}.to have_enqueued_mail(MyMailer, :my_particular_email).once
Run Code Online (Sandbox Code Playgroud)