How to Check What Is Queued in Activejob Using Rspec

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,

  1. First, put the job in the queue with perform_later.
  2. Get the parent job details from the queue currently_queued_job = enqueued_jobs.first.
  3. 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



Leave a reply



Submit