In Rails' Minitest helpers, how do assert_enqueued_with, perform_enqueued_jobs and assert_performed_jobs differ, and why might a model test lack them?
answer
- enqueued versus performed
- block form diffs the job list
- perform_enqueued_jobs with and without a block
- ActiveJob::TestHelper is not in ActiveSupport::TestCase
- ArgumentError without the test adapter
basics
~20 sassert_enqueued_with checks that a job with given class, args, queue or time was enqueued; perform_enqueued_jobs runs enqueued jobs; assert_performed_jobs counts jobs that ran. They live in ActiveJob::TestHelper, which integration, job and mailer tests include but plain model tests do not.
solid answer
~40 sThese helpers come from `ActiveJob::TestHelper` and work against the in-memory test adapter. `assert_enqueued_with(job:, args:, at:, queue:, priority:)` finds a matching enqueued job, among those enqueued in its block if given one, and returns the job instance. `assert_enqueued_jobs n` counts, optionally filtered with `only:`, `except:` or `queue:`. `perform_enqueued_jobs` runs jobs: with a block it performs every job enqueued inside it as it is enqueued, including scheduled ones and jobs those jobs enqueue; without a block it performs the jobs already enqueued. `assert_performed_jobs n` and `assert_performed_with` then check what ran. `ActiveJob::TestCase`, `ActionDispatch::IntegrationTest` and mailer tests include the module; `ActiveSupport::TestCase`, the base of model tests, does not, so a model test needs `include ActiveJob::TestHelper`. The assertions raise `ArgumentError` when the configured adapter is not the test adapter.
code
ruby · 16 linesclass StreakTest < ActiveSupport::TestCase
include ActiveJob::TestHelper # not included in model tests by default
test "finishing a session queues a streak update" do
user = users(:mia)
job = assert_enqueued_with(job: StreakUpdateJob, args: [user], queue: "default") do
user.meditation_sessions.create!(minutes: 10)
end
assert_equal user, job.arguments.first
perform_enqueued_jobs
assert_performed_jobs 1
assert_equal 1, user.reload.streak_days
end
endgo deeper
Use assert_enqueued_with with the job class and arguments in a request test, and perform_enqueued_jobs when the job's effect must be checked.
Explain block versus block-less forms, argument serialisation in matching, and which Rails test classes include ActiveJob::TestHelper.
Diagnose helpers that raise because of the adapter, and split enqueue assertions from behaviour assertions so tests stay precise.
Decide how much of the job pipeline the suite performs in-process versus only asserting enqueues, weighing speed against confidence.
## Enqueued versus performed Active Job separates **enqueueing** a job (`perform_later` hands it to the adapter) from **performing** it (a worker calls `perform`). In tests, Rails uses `ActiveJob::QueueAdapters::TestAdapter`, which stores jobs in two in-memory lists, `enqueued_jobs` and `performed_jobs`, instead of talking to a real backend. `ActiveJob::TestHelper` provides assertions over those lists and a way to run the queued jobs. | Helper | Looks at | Typical use | |---|---|---| | `assert_enqueued_jobs(n, only:, except:, queue:)` | enqueued list | count of jobs, optionally filtered | | `assert_no_enqueued_jobs` | enqueued list | nothing was queued | | `assert_enqueued_with(job:, args:, at:, queue:, priority:)` | enqueued list | one specific job | | `perform_enqueued_jobs(only:, except:, queue:, at:)` | runs jobs | execute queued work | | `assert_performed_jobs(n, ...)` / `assert_no_performed_jobs` | performed list | count of jobs that ran | | `assert_performed_with(job:, args:, ...)` | performed list | one specific job ran | ## Block and block-less forms Most helpers accept a block. With a block, they look only at what the block caused: `assert_enqueued_with` copies the enqueued list before the block, runs it, and searches only the jobs added. Without a block, they search everything enqueued so far in the test. The helper clears both lists before each test, so the block-less form is safe at the start of a test but becomes ambiguous when several steps enqueue jobs. `assert_enqueued_with` returns the matching job instance, so a test can inspect it further. Its failure message lists the jobs that were enqueued, which usually shows the mistake at once. ## Matching arguments and times - `args:` is compared after Active Job serialises and deserialises the arguments, so an Active Record model passed to `perform_later` comes back as the same record via its GlobalID. - Keyword arguments are given as a hash inside the array: `args: [{ user: user, plan: "trial" }]`. - Any expected value can be a lambda, for example `args: ->(args) { args.first.trial? }`. - `at:` with a time matches within one second either side, which absorbs rounding from `wait: 7.days`. - `queue:` accepts a symbol or a string. ## Running the jobs `perform_enqueued_jobs` has two modes: 1. **With a block**, it switches the adapter to perform jobs immediately as they are enqueued, including jobs scheduled for later and jobs that the performed jobs enqueue, then restores the adapter. `only:`, `except:`, `queue:` and `at:` filter what runs. 2. **Without a block**, it performs the jobs already in the enqueued list, moving each to the performed list. With `at:`, only jobs scheduled at or before that time run; without it, scheduled jobs run too. After either, `assert_performed_jobs` and `assert_performed_with` check what ran, and the job's real effects, such as a changed record, can be asserted directly. ## Which test classes have them The module is not part of `ActiveSupport::TestCase`. It reaches test classes in three ways: - `ActiveJob::TestCase`, the base of generated job tests, includes it. - `ActionDispatch::IntegrationTest` gets it through Active Job's railtie. - `ActionMailer::TestHelper` includes it, so mailer tests have it as well. A model test that calls `assert_enqueued_with` therefore fails with `NoMethodError` until the class adds `include ActiveJob::TestHelper`. ## The adapter requirement Every assertion first checks that `ActiveJob::Base.queue_adapter` is the test adapter and raises `ArgumentError`, naming the adapter in use, when it is not. In the test environment Rails falls back to the test adapter when nothing is configured; since Rails 7.2 an adapter set explicitly, for example in `config/application.rb`, also applies to tests and makes these helpers raise. A test class can also override `queue_adapter_for_test` to supply its own adapter. ## Choosing between them - To prove that a request **schedules** work, assert the enqueue with the class and arguments. - To prove the work **does the right thing**, perform it and assert the outcome, not merely that it ran. - Keep one test per concern: enqueue assertions in the request test, behaviour in the job test.
- What does perform_enqueued_jobs do with a job scheduled for next week?Both modes run it unless filtered. The block form sets the adapter to perform scheduled jobs immediately; the block-less form performs every enqueued job unless you pass `at:`, which limits it to jobs scheduled at or before that time. The job's own time checks still see the test's current time, so combine it with time travel when the job's logic depends on the date.
- Why does assert_enqueued_with raise ArgumentError in a suite that sets config.active_job.queue_adapter in application.rb?Since Rails 7.2 tests respect an explicitly configured adapter, so jobs go to that adapter instead of the test adapter. The helpers check for `ActiveJob::QueueAdapters::TestAdapter` and raise `ArgumentError` naming the adapter in use. Set the adapter per environment, leaving the test environment on `:test`.
- ActiveSupport::TestCase includes ActiveJob::TestHelper, so model tests have assert_enqueued_with
- perform_enqueued_jobs only performs jobs whose scheduled time has passed
- assert_enqueued_with compares a model argument by object identity
- assert_performed_jobs counts jobs that were merely enqueued
- The job helpers work with any configured queue adapter