We can check enqueued_jobs.length
, provided that email is the only possible type of background job.
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
Is there a better way to assert that:
MyMailer.my_particular_email
was enqueued,# first filter MyMailer job
my_mail_jobs = ActiveJob::Base.queue_adapter.enqueued_jobs.select { |job|
job[:job] == SendEmailsJob &&
job[:args][0] == "MyMailer"
}
# check only once
expect(my_mail_jobs.length).to eq(1)
# and that send to your particular email not other email
expect(my_mail_jobs.first[:args]).to include("your particular email")