instance method assert_enqueued_jobs

Ruby on Rails edge

Since v4.2.11.3

Available in: v4.2.11.3 v5.0.7.2 v5.1.7 v5.2.8.1 v6.0.6.1 v6.1.7.10 v7.0.10 v7.1.6 v7.2.4 v8.0.5.1 v8.1.4 edge

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 accept Class, Array of Class, or Proc. When passed a Proc, a hash containing the job’s class and its 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 : (Integer, ?only: untyped, ?except: untyped, ?queue: (String | Symbol)?) ?{ () -> void } -> true

Parameters

number req
only key = nil
except key = nil
queue key = nil
block block
Source
# File activejob/lib/active_job/test_helper.rb, line 164
    def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil, &block)
      require_active_job_test_adapter!("assert_enqueued_jobs")

      if block_given?
        original_jobs = enqueued_jobs_with(only: only, except: except, queue: queue)

        _assert_nothing_raised_or_warn("assert_enqueued_jobs", &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 164 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveJob::TestHelper

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close