Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions docs/src/content/docs/basics/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ We can unit test this piece of middleware in isolation using `package:test` and
`package:mocktail` just like before.

To test this, we need to import our middleware, create a mock `RequestContext`
using `package:mocktail`, apply our middleware to a dummy handler, and invoke
the handler with a mock request context. Then, we can assert that the simple
handler we applied the middleware to had access to the provided value.
using `package:mocktail`, stub `context.provide` (which the `provider` middleware
calls under the hood to inject the value), apply our middleware to a dummy
handler, and invoke the handler with the mock request context. Then, we can
capture the value the middleware provided and assert that it matches what we
expect.

```dart
import 'package:dart_frog/dart_frog.dart';
Expand All @@ -150,30 +152,30 @@ void main() {
group('middleware', () {
test('provides greeting', () async {
// Arrange
String? greeting;
final handler = middleware(
(context) {
greeting = context.read<String>();
return Response(body: '');
},
);
final handler = middleware((_) => Response(body: ''));
final request = Request.get(Uri.parse('http://localhost/'));
final context = _MockRequestContext();
when(() => context.request).thenReturn(request);
when(() => context.provide<String>(any())).thenReturn(context);

// Act
await handler(context);

// Assert
expect(greeting, equals('Hello World'));
final create = verify(() => context.provide<String>(captureAny()))
.captured
.single as String Function();
expect(create(), equals('Hello World'));
});
});
}
```

:::note

We are stubbing the `context.request` with a real `Request` object so that the
`provider` is able to inject the value.
The `provider` middleware injects values by calling `context.provide`, so we
stub it to return the mocked `context` and capture the callback it was given.
Invoking that captured callback lets us assert the value the middleware
provides. (We also stub `context.request` with a real `Request` object.)

:::