How to check what is queued in ActiveJob using Rspec
The accepted answer no longer works for me, so I tried Michael H.'s suggestion in the comments, which works.
describe 'whatever' do
include ActiveJob::TestHelper
after do
clear_enqueued_jobs
end
it 'should email' do
expect(enqueued_jobs.size).to eq(1)
end
end
How to test an ActiveJob callback?
I suspect that the issue here is that the perform_enqueued_jobs
helper attempts to execute all jobs in the queue until that point, but the queue list is never exhausted, because each job queues another job (hence the loop).
Try this,
- First, put the job in the queue with
perform_later
. - Get the parent job details from the queue
currently_queued_job = enqueued_jobs.first
. - Call
perform_enqueued_jobs
but use the:only
option to filter/limit the jobs that will get executed. For instance,
perform_enqueued_jobs(
only: ->(job) { job['job_id'] == currently_queued_job['job_id'] }
)
Here's the Rails documentation for the perform_enqueued_jobs
, mentioning:
:only
and:except
options accepts Class, Array of Class or Proc. When passed a Proc, an instance of the job will be passed as argument.
Related Topics
Error Installing Any Ruby Version with Rvm on Osx
How to Check If a Number Is Included in a Range (In One Statement)
Profile a Rails Controller Action
Differencebetween Methods and Attributes in Ruby
Ruby Method with Maximum Number of Parameters
How to Use "Gets" on a Rake Task
Puts Doesn't Print Stuff to Console
How to Monitor Uptime of 20 Websites (Ping or Http) in Node.Js/Ror
How to Return Multiple Tags from a Rails Helper
Ruby Koans: Why Convert List of Symbols to Strings
Best Practice About Empty Belongs_To Association
How to Update Gems on Production Server
Building a Windows Executable from My Ruby App
How to Reflect in the Database a New Belongs_To and Has_Many Relationship in Ruby on Rails
How to Use Define_Method Inside Initialize()