Method: ActiveJob::TestHelper#assert_enqueued_with

Defined in:
lib/active_job/test_helper.rb

#assert_enqueued_with(args = {}) ⇒ Object

Asserts that the job passed in the block has been enqueued with the given arguments.

def test_assert_enqueued_with
  assert_enqueued_with(job: MyJob, args: [1,2,3], queue: 'low') do
    MyJob.perform_later(1,2,3)
  end

  assert_enqueued_with(job: MyJob, at: Date.tomorrow.noon) do
    MyJob.set(wait_until: Date.tomorrow.noon).perform_later
  end
end


235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/active_job/test_helper.rb', line 235

def assert_enqueued_with(args = {})
  original_enqueued_jobs_count = enqueued_jobs.count
  args.assert_valid_keys(:job, :args, :at, :queue)
  serialized_args = serialize_args_for_assertion(args)
  yield
  in_block_jobs = enqueued_jobs.drop(original_enqueued_jobs_count)
  matching_job = in_block_jobs.find do |job|
    serialized_args.all? { |key, value| value == job[key] }
  end
  assert matching_job, "No enqueued job found with #{args}"
  instantiate_job(matching_job)
end