SwitchUp SwitchUp Ranked Best Coding Bootcamps 2025

How to test ActionMailer deliver_later with rspec

Altcademy Team wrote on 7 February 2018

You can get all enqueue_jobs from ActiveJob

ActiveJob::Base.queue_adapter.enqueued_jobs

The code above will return all the enqueue jobs, which returns an array of objects

[ { :job: ExampleJob, :args: # whatever args, :queue: name_of_the_queue },{ :job: AnotherExampleJob, :args: # whatever args, :queue: name_of_another_queue } ]

To filter different jobs (ie. ActionMailer async with deliver_later!), you can

ActionMailer::DeliveryJob.queue_adapter.enqueued_jobs.count{ |x| x[:job] == ActionMailer::DeliveryJob} # In Rspec, for example, expect(ActionMailer::DeliveryJob.queue_adapter.enqueued_jobs.count{ |x| x[:job] == ActionMailer::DeliveryJob}).to eq(1) # expecting 1 async email

Trusted by

Students and instructors from world-class organizations

Imperial College London
Carnegie Mellon University
City University of Hong Kong
Hack Reactor
Cisco Meraki
University of Oxford
Swift
Bazaarvoice
Waterloo
Uber
AtlanTech
Tumblr
Boston College
Bombardier Aerospace
University of St. Andrews
New York University
Minerva Schools at KGI
Merrill Lynch
Riot Games
JP Morgan
Morgan Stanley
Advanced Placement®
Google
KPMG
The University of Hong Kong
University of Toronto
SCMP
Moat
Zynga
Hello Toby
Deloitte
Goldman Sachs
Yahoo
HSBC
General Assembly
Tesla
McGill University
Microsoft

Join the upcoming Cohort #97

Enroll for January 6th, 2025