Test jobs¶
This guide shows you how to test jobs with RSpec or Minitest: how to unit-test a job's work, how to check that your code enqueues a job, and how to run enqueued jobs inside a test. You don't need a worker process or a database queue. For the full list of matchers and helpers, see Testing helpers in the mankai-jobs reference.
Use the memory backend in tests¶
The memory backend keeps jobs in memory until your test runs them. It doesn't store anything or run
anything in the background, so each test decides when jobs run. To use it in tests, set the backend
in config/providers/queue.rb:
# config/providers/queue.rb
config.backend = Hanami.env?(:test) ? :memory : Mankai::Jobs::SQL.backend
The testing helpers work only with the memory backend.
Test a job directly¶
A job is a component, so the fastest way to test its work is to resolve it and call it. The following
spec tests both outcomes of the jobs.send_welcome job:
# spec/jobs/send_welcome_spec.rb
RSpec.describe Bookshelf::Jobs::SendWelcome do
subject(:job) { Hanami.app["jobs.send_welcome"] }
it "delivers a welcome email" do
expect(job.call(user_id: 1)).to be_success
end
it "fails with :user_missing for an unknown user" do
expect(job.call(user_id: 0)).to eq(Dry::Monads::Failure(:user_missing))
end
end
The job's Deps collaborators resolve as they do in the app. To isolate the job, stub its mailer or
repository as you would for any Hanami component.
Calling the job directly runs only #call and returns its raw result. It doesn't apply the contract,
callbacks, or rules such as discard_on :user_missing. To test those, enqueue the job and run it; see
Run enqueued jobs.
Include the test helpers¶
To use the matchers and helpers, require mankai/jobs/testing and include Mankai::Jobs::Testing in
RSpec. Clear the backend before each test, so that one test never sees another test's jobs:
# spec/spec_helper.rb
require "mankai/jobs/testing"
RSpec.configure do |config|
config.include Mankai::Jobs::Testing
config.before { jobs_backend.clear }
end
jobs_backend returns the app's queue.backend. If your provider has another name, override the
method:
Check that code enqueues a job¶
To check that a block enqueues a job, use the enqueue_job matcher. The following spec checks that
signing up enqueues one welcome email on the mailers queue:
expect { post "/signup", email: "[email protected]" }
.to enqueue_job("jobs.send_welcome").with(user_id: 1).on_queue("mailers").once
To check every job enqueued since the backend was last cleared, use the have_enqueued_job matcher
on the backend:
Neither matcher runs the job. Both accept the following chain methods:
| Chain method | Matches |
|---|---|
.with(user_id: 1) |
Exactly these arguments, as stored. Give every argument, not a subset. |
.on_queue("mailers") |
The queue, as stored. With a queue_prefix, give the prefixed name, such as "staging_mailers". |
.with_priority(5) |
The priority. |
.at(time) |
The scheduled time, within a millisecond, or a Range of times. |
.once, .twice, .exactly(n) |
The number of matching jobs. Without one of these, at least one. |
To check that a block doesn't enqueue a job, negate the matcher:
expect { post "/signup", email: "[email protected]" }.not_to enqueue_job("jobs.send_digest")
Run enqueued jobs¶
To run the jobs that a block enqueues, wrap the block in perform_enqueued_jobs. It also runs the
jobs that those jobs enqueue, and returns their outcomes:
outcomes = perform_enqueued_jobs do
post "/signup", email: "[email protected]"
end
expect(outcomes.map(&:status)).to eq([:succeeded])
The runner applies the contract, callbacks, and rules, as a worker does. Without a block,
perform_enqueued_jobs runs every due job. jobs_backend.drain does the same on the backend, and
jobs_backend.failed returns the outcomes of the jobs that failed.
A job is due when its scheduled time has passed. A job enqueued with wait: or at:, or a retry
that's waiting for its backoff, isn't due yet, so perform_enqueued_jobs skips it. To run it, pass a
later time as now::
enqueuer.call("jobs.send_reminder", {user_id: 1}, wait: 3600)
perform_enqueued_jobs(now: Time.now + 3600)
Use Minitest assertions¶
For Minitest, include Mankai::Jobs::Testing::Assertions in your test class. It also includes
Mankai::Jobs::Testing, so jobs_backend and perform_enqueued_jobs are available. The following
assertions are available:
include Mankai::Jobs::Testing::Assertions
assert_enqueued_job("jobs.send_welcome", {user_id: 1}, queue: "mailers") do
post "/signup", email: "[email protected]"
end
assert_enqueued_job("jobs.send_welcome", priority: 0, at: Time.now..Time.now + 60, count: 1)
assert_enqueued_jobs(2) { post "/signup", email: "[email protected]" }
assert_enqueued_jobs(1, "jobs.send_welcome") { post "/signup", email: "[email protected]" }
assert_no_enqueued_jobs("jobs.send_welcome") { post "/login", email: "[email protected]" }
Without a block, an assertion checks every job enqueued since the backend was last cleared.
Test a slice's jobs¶
To test a slice's jobs, enqueue through the slice's enqueuer and match the slice-qualified key:
let(:enqueuer) { Admin::Slice["queue.enqueuer"] }
it "enqueues the import" do
expect { enqueuer.call("jobs.import", {name: "Ada"}) }
.to enqueue_job("admin:jobs.import").with(name: "Ada")
end
The slice shares the app's backend, so jobs_backend also holds the slice's jobs.
Decide what to test¶
Write tests for the following:
- The job's success, failure, discard, and retry paths.
- The action or operation that enqueues the job: the key, arguments, queue, and timing.
- The contract's handling of missing and invalid arguments.
- Uniqueness, if the job relies on it.
The memory backend ignores priorities, concurrency limits, and transactional jobs, and doesn't run recurring tasks, so tests with it don't exercise those SQL backend features. You don't need to test the backends themselves; the gems' own specs cover them. To check a job end to end on the SQL backend, enqueue it in development with a worker running, as in Run jobs on the SQL backend.
What's next¶
- Make jobs reliable: retries, unique jobs, and concurrency limits.
- Testing helpers: every matcher and helper.