Provides helper methods for testing Active Job

Namespace

Module

Methods

Instance Public methods

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

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, that 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

The number of times a specific job was enqueued can be asserted.

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

The number of times a job except specific class was enqueued can be asserted.

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

The number of times a job is enqueued to a specific queue can also be asserted.

def test_logging_job
  assert_enqueued_jobs 2, queue: 'default' do
    LoggingJob.perform_later
    HelloJob.perform_later('elfassy')
  end
end
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 118
    def assert_enqueued_jobs(number, only: nil, except: nil, queue: nil)
      if block_given?
        original_count = enqueued_jobs_size(only: only, except: except, queue: queue)
        yield
        new_count = enqueued_jobs_size(only: only, except: except, queue: queue)
        assert_equal number, new_count - original_count, "#{number} jobs expected, but #{new_count - original_count} were enqueued"
      else
        actual_count = enqueued_jobs_size(only: only, except: except, queue: queue)
        assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued"
      end
    end
🔎 See on GitHub

assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)

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
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 300
    def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
      original_enqueued_jobs_count = enqueued_jobs.count
      expected = { job: job, args: args, at: at, queue: queue }.compact
      expected_args = prepare_args_for_assertion(expected)
      yield
      in_block_jobs = enqueued_jobs.drop(original_enqueued_jobs_count)
      matching_job = in_block_jobs.find do |in_block_job|
        deserialized_job = deserialize_args_for_assertion(in_block_job)
        expected_args.all? { |key, value| value == deserialized_job[key] }
      end
      assert matching_job, "No enqueued job found with #{expected}"
      instantiate_job(matching_job)
    end
🔎 See on GitHub

assert_no_enqueued_jobs(only: nil, except: nil, &block)

Asserts that no jobs have been enqueued.

def test_jobs
  assert_no_enqueued_jobs
  HelloJob.perform_later('jeremy')
  assert_enqueued_jobs 1
end

If a block is passed, that block should not cause any job to be enqueued.

def test_jobs_again
  assert_no_enqueued_jobs do
    # No job should be enqueued from this block
  end
end

It can be asserted that no jobs of a specific kind are enqueued:

def test_no_logging
  assert_no_enqueued_jobs only: LoggingJob do
    HelloJob.perform_later('jeremy')
  end
end

It can be asserted that no jobs except specific class are enqueued:

def test_no_logging
  assert_no_enqueued_jobs except: HelloJob do
    HelloJob.perform_later('jeremy')
  end
end

Note: This assertion is simply a shortcut for:

assert_enqueued_jobs 0, &block
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 165
    def assert_no_enqueued_jobs(only: nil, except: nil, &block)
      assert_enqueued_jobs 0, only: only, except: except, &block
    end
🔎 See on GitHub

assert_no_performed_jobs(only: nil, except: nil, &block)

Asserts that no jobs have been performed.

def test_jobs
  assert_no_performed_jobs

  perform_enqueued_jobs do
    HelloJob.perform_later('matthew')
    assert_performed_jobs 1
  end
end

If a block is passed, that block should not cause any job to be performed.

def test_jobs_again
  assert_no_performed_jobs do
    # No job should be performed from this block
  end
end

The block form supports filtering. If the :only option is specified, then only the listed job(s) will not be performed.

def test_no_logging
  assert_no_performed_jobs only: LoggingJob do
    HelloJob.perform_later('jeremy')
  end
end

Also if the :except option is specified, then the job(s) except specific class will not be performed.

def test_no_logging
  assert_no_performed_jobs except: HelloJob do
    HelloJob.perform_later('jeremy')
  end
end

Note: This assertion is simply a shortcut for:

assert_performed_jobs 0, &block
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 285
    def assert_no_performed_jobs(only: nil, except: nil, &block)
      assert_performed_jobs 0, only: only, except: except, &block
    end
🔎 See on GitHub

assert_performed_jobs(number, only: nil, except: nil)

Asserts that the number of performed jobs matches the given number. If no block is passed, perform_enqueued_jobs must be called around the job call.

def test_jobs
  assert_performed_jobs 0

  perform_enqueued_jobs do
    HelloJob.perform_later('xavier')
  end
  assert_performed_jobs 1

  perform_enqueued_jobs do
    HelloJob.perform_later('yves')
    assert_performed_jobs 2
  end
end

If a block is passed, that block should cause the specified number of jobs to be performed.

def test_jobs_again
  assert_performed_jobs 1 do
    HelloJob.perform_later('robin')
  end

  assert_performed_jobs 2 do
    HelloJob.perform_later('carlos')
    HelloJob.perform_later('sean')
  end
end

The block form supports filtering. If the :only option is specified, then only the listed job(s) will be performed.

def test_hello_job
  assert_performed_jobs 1, only: HelloJob do
    HelloJob.perform_later('jeremy')
    LoggingJob.perform_later
  end
end

Also if the :except option is specified, then the job(s) except specific class will be performed.

def test_hello_job
  assert_performed_jobs 1, except: LoggingJob do
    HelloJob.perform_later('jeremy')
    LoggingJob.perform_later
  end
end

An array may also be specified, to support testing multiple jobs.

def test_hello_and_logging_jobs
  assert_nothing_raised do
    assert_performed_jobs 2, only: [HelloJob, LoggingJob] do
      HelloJob.perform_later('jeremy')
      LoggingJob.perform_later('stewie')
      RescueJob.perform_later('david')
    end
  end
end
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 232
    def assert_performed_jobs(number, only: nil, except: nil)
      if block_given?
        original_count = performed_jobs.size
        perform_enqueued_jobs(only: only, except: except) { yield }
        new_count = performed_jobs.size
        assert_equal number, new_count - original_count,
          "#{number} jobs expected, but #{new_count - original_count} were performed"
      else
        performed_jobs_size = performed_jobs.size
        assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed"
      end
    end
🔎 See on GitHub

assert_performed_with(job: nil, args: nil, at: nil, queue: nil)

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

def test_assert_performed_with
  assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') do
    MyJob.perform_later(1,2,3)
  end

  assert_performed_with(job: MyJob, at: Date.tomorrow.noon) do
    MyJob.set(wait_until: Date.tomorrow.noon).perform_later
  end
end
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 325
    def assert_performed_with(job: nil, args: nil, at: nil, queue: nil)
      original_performed_jobs_count = performed_jobs.count
      expected = { job: job, args: args, at: at, queue: queue }.compact
      expected_args = prepare_args_for_assertion(expected)
      perform_enqueued_jobs { yield }
      in_block_jobs = performed_jobs.drop(original_performed_jobs_count)
      matching_job = in_block_jobs.find do |in_block_job|
        deserialized_job = deserialize_args_for_assertion(in_block_job)
        expected_args.all? { |key, value| value == deserialized_job[key] }
      end
      assert matching_job, "No performed job found with #{expected}"
      instantiate_job(matching_job)
    end
🔎 See on GitHub

perform_enqueued_jobs(only: nil, except: nil)

Performs all enqueued jobs in the duration of the block.

def test_perform_enqueued_jobs
  perform_enqueued_jobs do
    MyJob.perform_later(1, 2, 3)
  end
  assert_performed_jobs 1
end

This method also supports filtering. If the :only option is specified, then only the listed job(s) will be performed.

def test_perform_enqueued_jobs_with_only
  perform_enqueued_jobs(only: MyJob) do
    MyJob.perform_later(1, 2, 3) # will be performed
    HelloJob.perform_later(1, 2, 3) # will not be performed
  end
  assert_performed_jobs 1
end

Also if the :except option is specified, then the job(s) except specific class will be performed.

def test_perform_enqueued_jobs_with_except
  perform_enqueued_jobs(except: HelloJob) do
    MyJob.perform_later(1, 2, 3) # will be performed
    HelloJob.perform_later(1, 2, 3) # will not be performed
  end
  assert_performed_jobs 1
end
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 370
    def perform_enqueued_jobs(only: nil, except: nil)
      validate_option(only: only, except: except)
      old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs
      old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs
      old_filter = queue_adapter.filter
      old_reject = queue_adapter.reject

      begin
        queue_adapter.perform_enqueued_jobs = true
        queue_adapter.perform_enqueued_at_jobs = true
        queue_adapter.filter = only
        queue_adapter.reject = except
        yield
      ensure
        queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs
        queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs
        queue_adapter.filter = old_filter
        queue_adapter.reject = old_reject
      end
    end
🔎 See on GitHub

queue_adapter()

Accesses the queue_adapter set by ActiveJob::Base.

def test_assert_job_has_custom_queue_adapter_set
  assert_instance_of CustomQueueAdapter, HelloJob.queue_adapter
end
📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 396
    def queue_adapter
      ActiveJob::Base.queue_adapter
    end
🔎 See on GitHub

queue_adapter_for_test()

Specifies the queue adapter to use with all active job test helpers.

Returns an instance of the queue adapter and defaults to ActiveJob::QueueAdapters::TestAdapter.

Note: The adapter provided by this method must provide some additional methods from those expected of a standard ActiveJob::QueueAdapter in order to be used with the active job test helpers. Refer to ActiveJob::QueueAdapters::TestAdapter.

📝 Source code
# File activejob/lib/active_job/test_helper.rb, line 64
    def queue_adapter_for_test
      ActiveJob::QueueAdapters::TestAdapter.new
    end
🔎 See on GitHub