How to test whether Mailer is enqueued 【Ruby on Rails】

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"}]

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)