Skip to content

Add Queue.fake() for tests - #15

Merged
necessarylion merged 1 commit into
mainfrom
feat/queue-fake
Nov 2, 2026
Merged

necessarylion merged 1 commit into
mainfrom
feat/queue-fake

Conversation

@mydoxie

@mydoxie mydoxie commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Problem

Tests had no way to fake the queue. They had to use the sync driver, which really runs every job. So a test could not check "this job was dispatched" without running it.

Fix

  • Queue.fake() returns a QueueFake. While it is on, Queue.dispatch() (and job.dispatch()) keeps the job in a list. It does not run or save it. It needs no Queue.configure().
  • Checks: assertPushed<T>([test]), assertPushedTimes<T>(n), assertNotPushed<T>(), assertNothingPushed(). A failed check throws an AssertionError that says what was pushed.
  • Read the jobs: pushed (each PushedJob has job, queue and delay), jobsOf<T>(), pushedOf<T>(). clear() forgets them.
  • Queue.restore() stops the fake. This matches Mail.fake() / Mail.restore() of dox_mailer.
  • Mail.queue() and Mail.later() go through Queue.dispatch(), so the fake catches them too.
  • dispatchSync() is not faked. It still runs the job, like before.

No breaking change. No version bump. Changelog is under Unreleased. README and docs/queue.md have a new part in Testing.

Tests

  • New test/queue_fake_test.dart: each assert, with its failure message, queue name and delay, dispatchSync, Mail.queue() / Mail.later(), and restore.
  • dart analyze: no issues. dart test: all passed (97 passed, 90 skipped stores).

@mydoxie

mydoxie commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Bot review

Verdict: OK. Nothing is blocking.

What I checked:

  • Every way to push a job goes through Queue.dispatch(): job.dispatch(), Queue.dispatch() with delay and queue, encrypted jobs, and Mail.queue() / Mail.later() through QueueMailQueue. The fake catches all of them.
  • dispatchSync() still runs the job. Queue.restore() brings the real queue back.
  • No breaking change. No version bump. The changelog is under ## Unreleased.
  • The assert messages are clear. The tests cover pass and fail for each assert.
  • dart analyze: no issues. dart format: clean. dart test: 97 passed, 90 skipped. I ran it with the local dox-mailer on the branch of dox-mailer PR CI: test against the newest code of the other Dox repos #12, so the two fakes work together.
  • With both Mail.fake() (PR CI: test against the newest code of the other Dox repos #12) and Queue.fake() on, queued mail goes to the mail fake (mail.queued), not to queue.pushed. That is fine (Laravel works the same way), but see nit 1.

Nits (no fix needed):

  1. README / docs say the queue fake catches Mail.queue(). That is only true when Mail.fake() is off. One line could say: "When Mail.fake() is on too, queued mail goes to mail.queued."
  2. Queue.fake() calls Mail.useQueue(...), and Queue.restore() does not undo it. In an app this is already set by QueueProvider, so it is harmless. In a test file with no queue, a later Mail.queue() goes to the real Queue.dispatch() and throws "The queue is not set up" instead of the mailer error.
  3. The fake does not encode the job. So a job that is not in QueueConfig.jobs, or has data that is not JSON, passes in a faked test but fails for real. Laravel's fake is the same. A short note in the docs could help.
  4. Grammar in a message: "Expected a AllTypesJob job" and "pushed 1 times". Small, can stay.
  5. pushed is a public mutable list. clear() is there, so fine.

@necessarylion
necessarylion merged commit 47d2230 into main Nov 2, 2026
1 of 2 checks passed
@necessarylion
necessarylion deleted the feat/queue-fake branch November 2, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants