Skip to content

Commit 55f5757

Browse files
authored
docs(example): simplify /count implementation (#115)
1 parent 117f571 commit 55f5757

File tree

4 files changed

+8
-20
lines changed

4 files changed

+8
-20
lines changed

example/routes/count/_middleware.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import 'dart:developer';
2-
31
import 'package:dart_frog/dart_frog.dart';
42

5-
final _counter = Counter('counter', 'a simple request counter');
3+
int _count = 0;
64

75
Handler middleware(Handler handler) {
8-
return handler.use(provider<Counter>((_) => _counter));
6+
return handler.use(provider<int>((_) => ++_count));
97
}

example/routes/count/index.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import 'dart:developer';
2-
31
import 'package:dart_frog/dart_frog.dart';
42

53
Response onRequest(RequestContext context) {
6-
final counter = context.read<Counter>()..value += 1;
4+
final count = context.read<int>();
75
return Response(
8-
body: 'You have requested this route ${counter.value.toInt()} time(s).',
6+
body: 'You have requested this route $count time(s).',
97
);
108
}

example/test/routes/count/_middleware_test.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:developer';
2-
31
import 'package:dart_frog/dart_frog.dart';
42
import 'package:mocktail/mocktail.dart';
53
import 'package:test/test.dart';
@@ -10,12 +8,11 @@ class _MockRequestContext extends Mock implements RequestContext {}
108

119
void main() {
1210
group('middleware', () {
13-
test('provides count instance', () async {
14-
double? count;
11+
test('provides incremented count', () async {
12+
int? count;
1513
final handler = middleware(
1614
(context) {
17-
final counter = context.read<Counter>();
18-
count = counter.value += 1;
15+
count = context.read<int>();
1916
return Response(body: '');
2017
},
2118
);

example/test/routes/count/index_test.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import 'dart:developer';
21
import 'dart:io';
32

43
import 'package:dart_frog/dart_frog.dart';
@@ -9,16 +8,12 @@ import '../../../routes/count/index.dart' as route;
98

109
class _MockRequestContext extends Mock implements RequestContext {}
1110

12-
class _MockCounter extends Mock implements Counter {}
13-
1411
void main() {
1512
group('GET /', () {
1613
test('responds with a 200 and count.', () async {
1714
const count = 42;
18-
final counter = _MockCounter();
19-
when(() => counter.value).thenReturn(count.toDouble());
2015
final context = _MockRequestContext();
21-
when(() => context.read<Counter>()).thenReturn(counter);
16+
when(() => context.read<int>()).thenReturn(count);
2217
final response = route.onRequest(context);
2318
expect(response.statusCode, equals(HttpStatus.ok));
2419
expect(

0 commit comments

Comments
 (0)