You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/basics/testing.md
+126-4Lines changed: 126 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,27 @@ sidebar_position: 5
4
4
5
5
# Testing 🧪
6
6
7
-
In Dart Frog, we can unit test our route handlers and middleware effectively because they are plain functions.
7
+
In Dart Frog, we can effectively unit test our route handlers and middleware using [`package:test`](https://pub.dev/packages/test) and [`package:mocktail`](https://pub.dev/packages/mocktail).
8
8
9
-
For example, we can test our route handler above using `package:test`:
9
+
## Route Handlers 🚏
10
+
11
+
Testing route handlers is pretty straightforward and doesn't require any new concepts because a route handler is just a plain Dart function.
12
+
13
+
### Basics
14
+
15
+
Let's take a look at how we can test the following route handler:
16
+
17
+
```dart
18
+
import 'package:dart_frog/dart_frog.dart';
19
+
20
+
Response onRequest(RequestContext context) {
21
+
return Response(body: 'Hello World');
22
+
}
23
+
```
24
+
25
+
In the above handler, we're simply returning a `200` response with `'Hello World'` in the response body.
26
+
27
+
To test this, we can import our route handler, create a mock `RequestContext` using `package:mocktail`, and invoke `onRequest` with the mock request context to get a `Response`. Then, we can assert that the response is what we expect. In this case, we're checking the `statusCode` and response `body` to ensure that the response is a `200` and the body equals `'Hello World'`.
In the above test, we're using `package:mocktail` to create a mock `RequestContext` and stub the return value when calling `context.read<String>()`. Then, all we need to do is call `onRequest` with the mocked context and we can assert that the response is what we expect. In this case, we're checking the statusCode and response body to ensure that the response is a 200 with the provided greeting.
102
+
## Middleware 🍔
103
+
104
+
Unit testing middleware is very similar to unit testing route handlers — they are both just dart functions after all!
105
+
106
+
### Basics
107
+
108
+
Let's take a look at a piece of middleware that provides a greeting to the `RequestContext` via the `provider` API:
We can unit test this piece of middleware in isolation using `package:test` and `package:mocktail` just like before.
119
+
120
+
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.
121
+
122
+
```dart
123
+
import 'package:dart_frog/dart_frog.dart';
124
+
import 'package:mocktail/mocktail.dart';
125
+
import 'package:test/test.dart';
126
+
127
+
import '../../routes/_middleware.dart';
128
+
129
+
class _MockRequestContext extends Mock implements RequestContext {}
130
+
131
+
void main() {
132
+
group('middleware', () {
133
+
test('provides greeting', () async {
134
+
// Arrange
135
+
String? greeting;
136
+
final handler = middleware(
137
+
(context) {
138
+
greeting = context.read<String>();
139
+
return Response(body: '');
140
+
},
141
+
);
142
+
final request = Request.get(Uri.parse('http://localhost/'));
143
+
final context = _MockRequestContext();
144
+
when(() => context.request).thenReturn(request);
145
+
146
+
// Act
147
+
await handler(context);
148
+
149
+
// Assert
150
+
expect(greeting, equals('Hello World'));
151
+
});
152
+
});
153
+
}
154
+
```
155
+
156
+
:::info
157
+
We are stubbing the `context.read` with a real `Request` object so that the `provider` is able to inject the value.
0 commit comments