instance method assert_enqueued_jobs

Ruby on Rails 6.1.7.10

Since v4.2.9

Available in: v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block)

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 accepts 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

Parameters

number req
only key = nil
except key = nil
queue key = nil
block block
Source
# File activejob/lib/active_job/test_helper.rb, line 123
    def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block)
      if block_given?
        original_jobs = enqueued_jobs_with(only: only, except: except, queue: queue)

        assert_nothing_raised(&block)

        new_jobs = enqueued_jobs_with(only: only, except: except, queue: queue)

        actual_count = (new_jobs - original_jobs).count
      else
        actual_count = enqueued_jobs_with(only: only, except: except, queue: queue).count
      end

      assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued"
    end

Defined in activejob/lib/active_job/test_helper.rb line 123 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveJob::TestHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close