method

assert_enqueued_jobs

Importance_2
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Class: TestHelper
assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block) public

Asserts that the number of enqueued jobs matches the given number.

def test_jobs
  assert_enqueued_jobs 0
  HelloJob.perform_later('david')
  assert_enqueued_jobs 1
  HelloJob.perform_later('abdelkader')
  assert_enqueued_jobs 2
end

If a block is passed, asserts that the block will cause the specified number of jobs to be enqueued.

def test_jobs_again
  assert_enqueued_jobs 1 do
    HelloJob.perform_later('cristian')
  end

  assert_enqueued_jobs 2 do
    HelloJob.perform_later('aaron')
    HelloJob.perform_later('rafael')
  end
end

Asserts the number of times a specific job was enqueued by passing :only option.

def test_logging_job
  assert_enqueued_jobs 1, only: LoggingJob do
    LoggingJob.perform_later
    HelloJob.perform_later('jeremy')
  end
end

Asserts the number of times a job except specific class was enqueued by passing :except option.

def test_logging_job
  assert_enqueued_jobs 1, except: HelloJob do
    LoggingJob.perform_later
    HelloJob.perform_later('jeremy')
  end
end

:only and :except options accept Class, Array of Class, or Proc. When passed a Proc, a hash containing the job’s class and it’s argument are passed as argument.

Asserts the number of times a job is enqueued to a specific queue by passing :queue option.

def test_logging_job
  assert_enqueued_jobs 2, queue: 'default' do
    LoggingJob.perform_later
    HelloJob.perform_later('elfassy')
  end
end
Show source
Register or log in to add new notes.