docs: fix middleware testing example#1968
Open
Yusufihsangorgel wants to merge 1 commit into
Open
Conversation
Yusufihsangorgel
requested review from
alestiago,
erickzanardo,
felangel,
marcossevilla,
renancaraujo and
wolfenrain
as code owners
July 8, 2026 17:46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
docs: fix broken middleware testing example
What was broken
The middleware test on the Testing
page does not compile-and-pass as written. Running it throws:
The
providermiddleware injects its value by callingcontext.provide(...):RequestContext.providereturns a (non-nullable)RequestContext. The exampleonly stubbed
context.request, nevercontext.provide, so on the mock contextprovidereturnednull. Thatnullis then passed straight into the wrappedhandler, which expects a
RequestContext, producing the type error above. Theold example's
context.read<String>()line is never reached.The fix
Stub
context.provide<String>to return the mocked context, then capture thecallback the
providerhanded it and assert that invoking it yields the expectedvalue. This is the same pattern the repo's own examples already use
(
examples/kitchen_sink/test/routes/_middleware_test.dart,examples/counter/test/routes/_middleware_test.dart), so the docs now match themaintained code.
The surrounding prose and the
:::notewere updated to explain thecontext.providestub, which is the crux of the fix.Evidence
Reproduced against
packages/dart_frog(v1.2.6) with a scratch project using theverbatim documented example.
Before (documented example, verbatim):
After (corrected example):
The corrected test is not a tautology: changing the middleware to provide a
different value (e.g.
'Goodbye') makes it fail withExpected: 'Hello World' Actual: 'Goodbye', confirming it exercises themiddleware's provided value rather than a hard-coded stub.
This fixes the broken example reported in #1963.
Note: #1963 also raises a broader request (a public
RequestContextconstructor / mock-free testing). This PR only corrects the documented example and intentionally does not close that discussion — so #1963 can stay open for the API question.