How to test whether Mailer is enqueued 【Ruby on Rails】
Oct 16, 2021
Suppose we develop mailer functions and want to test it, what should we do?
Useful way
The answer is below. The code is following the library above.
RSpec.describe 'test', type: :request do
include ActiveJob::TestHelper
context 'test1' do
def call
# ....some mailer functions
end it 'send an email' do
expect { call }.to have_enqueued_job.on_queue('mailers')
expect(enqueued_jobs.size).to eq 1
end
end
end
have_enqueued_job returns…
[{:id=>"xxxxxxxxxxxxxxxx", :job=>ActionMailer::DeliveryJob, :args=>["Mailer", "method", "deliver_now", {"_aj_globalid"=>"gid://product/class/:id"}], :queue=>"mailers"}]