module Minitest::Rails::Expectations::ActiveJob

This exists as a module to allow easy mixing into classes other than ActiveJob::TestCase where you might want to do job testing e.g. in an Active Record model which triggers jobs in a callback.

Public Instance Methods

must_enqueue_jobs(number) click to toggle source

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

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

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

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

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

See also ActiveJob::TestCase#assert_enqueued_jobs

# File lib/minitest/rails/expectations/active_job.rb, line 41
        
must_enqueue_with(args) click to toggle source

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

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

See also Minitest::Rails::Expectations#assert_enqueued_with

# File lib/minitest/rails/expectations/active_job.rb, line 148
        
must_perform_jobs(number) click to toggle source

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

def test_jobs
  must_perform_jobs 0

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

  perform_enqueued_jobs do
    HelloJob.perform_later('yves')
    must_perform_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
  must_perform_jobs 1 do
    HelloJob.perform_later('robin')
  end

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

See also ActiveJob::TestCase#assert_performed_jobs

# File lib/minitest/rails/expectations/active_job.rb, line 105
        
must_perform_with(args) click to toggle source

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

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

See also Minitest::Rails::Expectations#assert_performed_with

# File lib/minitest/rails/expectations/active_job.rb, line 162
extend ::ActiveSupport::Concern
wont_enqueue_jobs(number) click to toggle source

Expects that no jobs have been enqueued.

def test_jobs
  wont_enqueue_jobs
  HelloJob.perform_later('jeremy')
  must_enqueue_jobs 1
end

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

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

Note: This expectation is simply a shortcut for:

must_enqueue_jobs 0, &block

See also ActiveJob::TestCase#refute_enqueued_jobs

# File lib/minitest/rails/expectations/active_job.rb, line 67
        
wont_perform_jobs(number) click to toggle source

Expects that no jobs have been performed.

def test_jobs
  wont_perform_jobs

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

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

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

Note: This assertion is simply a shortcut for:

must_perform_jobs 0, &block

See also ActiveJob::TestCase#refute_performed_jobs

# File lib/minitest/rails/expectations/active_job.rb, line 134