From a0064f2bb7e019777550de34b0db240504912caf Mon Sep 17 00:00:00 2001 From: valentinallavayol Date: Mon, 9 Oct 2023 17:30:59 -0300 Subject: [PATCH] test: fixed broken tests due to upgrade --- flutter_news_example/api/pubspec.lock | 156 +++++++++--------- .../api/test/routes/_middleware_test.dart | 12 +- .../news_data_source_provider_test.dart | 11 +- .../src/middleware/user_provider_test.dart | 40 ++++- 4 files changed, 129 insertions(+), 90 deletions(-) diff --git a/flutter_news_example/api/pubspec.lock b/flutter_news_example/api/pubspec.lock index 3dea440fd..03a4a156a 100644 --- a/flutter_news_example/api/pubspec.lock +++ b/flutter_news_example/api/pubspec.lock @@ -5,34 +5,34 @@ packages: dependency: transitive description: name: _fe_analyzer_shared - sha256: ae92f5d747aee634b87f89d9946000c2de774be1d6ac3e58268224348cd0101a + sha256: eb376e9acf6938204f90eb3b1f00b578640d3188b4c8a8ec054f9f479af8d051 url: "https://pub.dev" source: hosted - version: "61.0.0" + version: "64.0.0" analyzer: dependency: transitive description: name: analyzer - sha256: ea3d8652bda62982addfd92fdc2d0214e5f82e43325104990d4f4c4a2a313562 + sha256: "69f54f967773f6c26c7dcb13e93d7ccee8b17a641689da39e878d5cf13b06893" url: "https://pub.dev" source: hosted - version: "5.13.0" + version: "6.2.0" args: dependency: transitive description: name: args - sha256: "4cab82a83ffef80b262ddedf47a0a8e56ee6fbf7fe21e6e768b02792034dd440" + sha256: eef6c46b622e0494a36c5a12d10d77fb4e855501a91c1b9ef9339326e58f0596 url: "https://pub.dev" source: hosted - version: "2.4.0" + version: "2.4.2" async: dependency: transitive description: name: async - sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0 + sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" url: "https://pub.dev" source: hosted - version: "2.10.0" + version: "2.11.0" boolean_selector: dependency: transitive description: @@ -45,10 +45,10 @@ packages: dependency: transitive description: name: build - sha256: "3fbda25365741f8251b39f3917fb3c8e286a96fd068a5a242e11c2012d495777" + sha256: "80184af8b6cb3e5c1c4ec6d8544d27711700bc3e6d2efad04238c7b5290889f0" url: "https://pub.dev" source: hosted - version: "2.3.1" + version: "2.4.1" build_config: dependency: transitive description: @@ -69,10 +69,10 @@ packages: dependency: transitive description: name: build_resolvers - sha256: db49b8609ef8c81cca2b310618c3017c00f03a92af44c04d310b907b2d692d95 + sha256: "64e12b0521812d1684b1917bc80945625391cb9bdd4312536b1d69dcb6133ed8" url: "https://pub.dev" source: hosted - version: "2.2.0" + version: "2.4.1" build_runner: dependency: "direct dev" description: @@ -85,10 +85,10 @@ packages: dependency: transitive description: name: build_runner_core - sha256: "14febe0f5bac5ae474117a36099b4de6f1dbc52df6c5e55534b3da9591bf4292" + sha256: c9e32d21dd6626b5c163d48b037ce906bbe428bc23ab77bcd77bb21e593b6185 url: "https://pub.dev" source: hosted - version: "7.2.7" + version: "7.2.11" built_collection: dependency: transitive description: @@ -101,34 +101,34 @@ packages: dependency: transitive description: name: built_value - sha256: "169565c8ad06adb760c3645bf71f00bff161b00002cace266cad42c5d22a7725" + sha256: a8de5955205b4d1dbbbc267daddf2178bd737e4bab8987c04a500478c9651e74 url: "https://pub.dev" source: hosted - version: "8.4.3" + version: "8.6.3" checked_yaml: dependency: transitive description: name: checked_yaml - sha256: "3d1505d91afa809d177efd4eed5bb0eb65805097a1463abdd2add076effae311" + sha256: feb6bed21949061731a7a75fc5d2aa727cf160b91af9a3e464c5e3a32e28b5ff url: "https://pub.dev" source: hosted - version: "2.0.2" + version: "2.0.3" code_builder: dependency: transitive description: name: code_builder - sha256: "0d43dd1288fd145de1ecc9a3948ad4a6d5a82f0a14c4fdd0892260787d975cbe" + sha256: "1be9be30396d7e4c0db42c35ea6ccd7cc6a1e19916b5dc64d6ac216b5544d677" url: "https://pub.dev" source: hosted - version: "4.4.0" + version: "4.7.0" collection: dependency: "direct main" description: name: collection - sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c" + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a url: "https://pub.dev" source: hosted - version: "1.17.1" + version: "1.18.0" convert: dependency: transitive description: @@ -141,18 +141,18 @@ packages: dependency: transitive description: name: coverage - sha256: "2fb815080e44a09b85e0f2ca8a820b15053982b2e714b59267719e8a9ff17097" + sha256: "595a29b55ce82d53398e1bcc2cba525d7bd7c59faeb2d2540e9d42c390cfeeeb" url: "https://pub.dev" source: hosted - version: "1.6.3" + version: "1.6.4" crypto: dependency: transitive description: name: crypto - sha256: aa274aa7774f8964e4f4f38cc994db7b6158dd36e9187aaceaddc994b35c6c67 + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab url: "https://pub.dev" source: hosted - version: "3.0.2" + version: "3.0.3" dart_frog: dependency: "direct main" description: @@ -165,10 +165,10 @@ packages: dependency: transitive description: name: dart_style - sha256: "7a03456c3490394c8e7665890333e91ae8a49be43542b616e414449ac358acd4" + sha256: abd7625e16f51f554ea244d090292945ec4d4be7bfbaf2ec8cccea568919d334 url: "https://pub.dev" source: hosted - version: "2.2.4" + version: "2.3.3" equatable: dependency: "direct main" description: @@ -181,10 +181,10 @@ packages: dependency: transitive description: name: file - sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d" + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" url: "https://pub.dev" source: hosted - version: "6.1.4" + version: "7.0.0" fixnum: dependency: transitive description: @@ -205,42 +205,42 @@ packages: dependency: transitive description: name: glob - sha256: "4515b5b6ddb505ebdd242a5f2cc5d22d3d6a80013789debfbda7777f47ea308c" + sha256: "0e7014b3b7d4dac1ca4d6114f82bf1782ee86745b9b42a92c9289c23d8a0ab63" url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" graphs: dependency: transitive description: name: graphs - sha256: f9e130f3259f52d26f0cfc0e964513796dafed572fa52e45d2f8d6ca14db39b2 + sha256: aedc5a15e78fc65a6e23bcd927f24c64dd995062bcd1ca6eda65a3cff92a4d19 url: "https://pub.dev" source: hosted - version: "2.2.0" + version: "2.3.1" hotreloader: dependency: transitive description: name: hotreloader - sha256: "66b78edf340352fcdd2043ab6116e58ec018c5968cab6485cde0f1769d28e0b8" + sha256: "728c0613556c1d153f7e7f4a367cffacc3f5a677d7f6497a1c2b35add4e6dacf" url: "https://pub.dev" source: hosted - version: "3.0.5" + version: "3.0.6" http: dependency: "direct main" description: name: http - sha256: "6aa2946395183537c8b880962d935877325d6a09a2867c3970c05c0fed6ac482" + sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2" url: "https://pub.dev" source: hosted - version: "0.13.5" + version: "0.13.6" http_methods: dependency: transitive description: name: http_methods - sha256: c192bb6fb4ae99d06053f67a2c1c65350a29bc778a39d9a12b96bd2ec820e9dc + sha256: "6bccce8f1ec7b5d701e7921dca35e202d425b57e317ba1a37f2638590e29e566" url: "https://pub.dev" source: hosted - version: "1.1.0" + version: "1.1.1" http_multi_server: dependency: transitive description: @@ -293,10 +293,10 @@ packages: dependency: transitive description: name: logging - sha256: "04094f2eb032cbb06c6f6e8d3607edcfcb0455e2bb6cbc010cb01171dcb64e6d" + sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340" url: "https://pub.dev" source: hosted - version: "1.1.1" + version: "1.2.0" matcher: dependency: transitive description: @@ -309,10 +309,10 @@ packages: dependency: transitive description: name: meta - sha256: "12307e7f0605ce3da64cf0db90e5fcab0869f3ca03f76be6bb2991ce0a55e82b" + sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e url: "https://pub.dev" source: hosted - version: "1.9.0" + version: "1.10.0" mime: dependency: transitive description: @@ -340,10 +340,10 @@ packages: dependency: transitive description: name: node_preamble - sha256: "8ebdbaa3b96d5285d068f80772390d27c21e1fa10fb2df6627b1b9415043608d" + sha256: "6e7eac89047ab8a8d26cf16127b5ed26de65209847630400f9aefd7cd5c730db" url: "https://pub.dev" source: hosted - version: "2.0.1" + version: "2.0.2" package_config: dependency: transitive description: @@ -372,26 +372,26 @@ packages: dependency: transitive description: name: pub_semver - sha256: "307de764d305289ff24ad257ad5c5793ce56d04947599ad68b3baa124105fc17" + sha256: "40d3ab1bbd474c4c2328c91e3a7df8c6dd629b79ece4c4bd04bee496a224fb0c" url: "https://pub.dev" source: hosted - version: "2.1.3" + version: "2.1.4" pubspec_parse: dependency: transitive description: name: pubspec_parse - sha256: "75f6614d6dde2dc68948dffbaa4fe5dae32cd700eb9fb763fe11dfb45a3c4d0a" + sha256: c63b2876e58e194e4b0828fcb080ad0e06d051cb607a6be51a9e084f47cb9367 url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.2.3" shelf: dependency: transitive description: name: shelf - sha256: c24a96135a2ccd62c64b69315a14adc5c3419df63b4d7c05832a346fdb73682c + sha256: ad29c505aee705f41a4d8963641f91ac4cee3c8fad5947e033390a7bd8180fa4 url: "https://pub.dev" source: hosted - version: "1.4.0" + version: "1.4.1" shelf_hotreload: dependency: transitive description: @@ -404,26 +404,26 @@ packages: dependency: transitive description: name: shelf_packages_handler - sha256: aef74dc9195746a384843102142ab65b6a4735bb3beea791e63527b88cc83306 + sha256: "89f967eca29607c933ba9571d838be31d67f53f6e4ee15147d5dc2934fee1b1e" url: "https://pub.dev" source: hosted - version: "3.0.1" + version: "3.0.2" shelf_static: dependency: transitive description: name: shelf_static - sha256: e792b76b96a36d4a41b819da593aff4bdd413576b3ba6150df5d8d9996d2e74c + sha256: a41d3f53c4adf0f57480578c1d61d90342cd617de7fc8077b1304643c2d85c1e url: "https://pub.dev" source: hosted - version: "1.1.1" + version: "1.1.2" shelf_web_socket: dependency: transitive description: name: shelf_web_socket - sha256: a988c0e8d8ffbdb8a28aa7ec8e449c260f3deb808781fe1284d22c5bba7156e8 + sha256: "9ca081be41c60190ebcb4766b2486a7d50261db7bd0f5d9615f2d653637a84c1" url: "https://pub.dev" source: hosted - version: "1.0.3" + version: "1.0.4" source_gen: dependency: transitive description: @@ -436,10 +436,10 @@ packages: dependency: transitive description: name: source_helper - sha256: "3b67aade1d52416149c633ba1bb36df44d97c6b51830c2198e934e3fca87ca1f" + sha256: "6adebc0006c37dd63fe05bca0a929b99f06402fc95aa35bf36d67f5c06de01fd" url: "https://pub.dev" source: hosted - version: "1.3.3" + version: "1.3.4" source_map_stack_trace: dependency: transitive description: @@ -460,26 +460,26 @@ packages: dependency: transitive description: name: source_span - sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250 + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" url: "https://pub.dev" source: hosted - version: "1.9.1" + version: "1.10.0" stack_trace: dependency: transitive description: name: stack_trace - sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" url: "https://pub.dev" source: hosted - version: "1.11.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 url: "https://pub.dev" source: hosted - version: "2.1.1" + version: "2.1.2" stream_transform: dependency: transitive description: @@ -540,10 +540,10 @@ packages: dependency: transitive description: name: typed_data - sha256: "26f87ade979c47a150c9eaab93ccd2bebe70a27dc0b4b29517f2904f04eb11a5" + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c url: "https://pub.dev" source: hosted - version: "1.3.1" + version: "1.3.2" very_good_analysis: dependency: "direct dev" description: @@ -556,41 +556,41 @@ packages: dependency: transitive description: name: vm_service - sha256: e7fb6c2282f7631712b69c19d1bff82f3767eea33a2321c14fa59ad67ea391c7 + sha256: c538be99af830f478718b51630ec1b6bee5e74e52c8a802d328d9e71d35d2583 url: "https://pub.dev" source: hosted - version: "9.4.0" + version: "11.10.0" watcher: dependency: transitive description: name: watcher - sha256: "6a7f46926b01ce81bfc339da6a7f20afbe7733eff9846f6d6a5466aa4c6667c0" + sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8" url: "https://pub.dev" source: hosted - version: "1.0.2" + version: "1.1.0" web_socket_channel: dependency: transitive description: name: web_socket_channel - sha256: ca49c0bc209c687b887f30527fb6a9d80040b072cc2990f34b9bec3e7663101b + sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b url: "https://pub.dev" source: hosted - version: "2.3.0" + version: "2.4.0" webkit_inspection_protocol: dependency: transitive description: name: webkit_inspection_protocol - sha256: "67d3a8b6c79e1987d19d848b0892e582dbb0c66c57cc1fef58a177dd2aa2823d" + sha256: "87d3f2333bb240704cd3f1c6b5b7acd8a10e7f0bc28c28dcf14e782014f4a572" url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.2.1" yaml: dependency: transitive description: name: yaml - sha256: "23812a9b125b48d4007117254bca50abb6c712352927eece9e155207b1db2370" + sha256: "75769501ea3489fca56601ff33454fe45507ea3bfb014161abc3b43ae25989d5" url: "https://pub.dev" source: hosted - version: "3.1.1" + version: "3.1.2" sdks: dart: ">=3.0.0 <4.0.0" diff --git a/flutter_news_example/api/test/routes/_middleware_test.dart b/flutter_news_example/api/test/routes/_middleware_test.dart index b3a865993..b4133939e 100644 --- a/flutter_news_example/api/test/routes/_middleware_test.dart +++ b/flutter_news_example/api/test/routes/_middleware_test.dart @@ -10,15 +10,23 @@ class _MockRequestContext extends Mock implements RequestContext {} void main() { group('middleware', () { test('provides NewsDataSource instance.', () async { + final context = _MockRequestContext(); + final handler = middleware( - (context) { + (_) { expect(context.read(), isNotNull); return Response(); }, ); final request = Request('GET', Uri.parse('http://127.0.0.1/')); - final context = _MockRequestContext(); + when(() => context.request).thenReturn(request); + + when(() => context.read()) + .thenReturn(InMemoryNewsDataSource()); + + when(() => context.provide(any())).thenReturn(context); + when(() => context.provide(any())).thenReturn(context); await handler(context); }); }); diff --git a/flutter_news_example/api/test/src/middleware/news_data_source_provider_test.dart b/flutter_news_example/api/test/src/middleware/news_data_source_provider_test.dart index bfd88c1af..3b612981a 100644 --- a/flutter_news_example/api/test/src/middleware/news_data_source_provider_test.dart +++ b/flutter_news_example/api/test/src/middleware/news_data_source_provider_test.dart @@ -9,15 +9,22 @@ void main() { group('newsDataSourceProvider', () { test('provides a NewsDataSource instance', () async { NewsDataSource? value; + final context = _MockRequestContext(); final handler = newsDataSourceProvider()( - (context) { + (_) { value = context.read(); return Response(body: ''); }, ); final request = Request.get(Uri.parse('http://localhost/')); - final context = _MockRequestContext(); when(() => context.request).thenReturn(request); + + when(() => context.read()) + .thenReturn(InMemoryNewsDataSource()); + + when(() => context.provide(any())).thenReturn(context); + when(() => context.provide(any())).thenReturn(context); + await handler(context); expect(value, isNotNull); }); diff --git a/flutter_news_example/api/test/src/middleware/user_provider_test.dart b/flutter_news_example/api/test/src/middleware/user_provider_test.dart index 1482e656f..d6bae32cf 100644 --- a/flutter_news_example/api/test/src/middleware/user_provider_test.dart +++ b/flutter_news_example/api/test/src/middleware/user_provider_test.dart @@ -5,21 +5,35 @@ import 'package:test/test.dart'; class _MockRequestContext extends Mock implements RequestContext {} +class _MockRequestUser extends Mock implements RequestUser {} + void main() { group('userProvider', () { + late RequestContext context; + + setUp(() { + context = _MockRequestContext(); + + when(() => context.provide(any())).thenReturn(context); + when(() => context.provide(any())).thenReturn(context); + }); + test( 'provides RequestUser.anonymous ' 'when authorization header is missing.', () async { RequestUser? value; final handler = userProvider()( - (context) { + (_) { value = context.read(); return Response(body: ''); }, ); + final request = Request.get(Uri.parse('http://localhost/')); - final context = _MockRequestContext(); + when(() => context.request).thenReturn(request); + when(() => context.read()).thenReturn(RequestUser.anonymous); + await handler(context); expect(value, equals(RequestUser.anonymous)); }); @@ -29,7 +43,7 @@ void main() { 'when authorization header is malformed (no bearer).', () async { RequestUser? value; final handler = userProvider()( - (context) { + (_) { value = context.read(); return Response(body: ''); }, @@ -38,9 +52,12 @@ void main() { Uri.parse('http://localhost/'), headers: {'Authorization': 'some token'}, ); - final context = _MockRequestContext(); + when(() => context.request).thenReturn(request); + when(() => context.read()).thenReturn(RequestUser.anonymous); + await handler(context); + expect(value, equals(RequestUser.anonymous)); expect(value!.isAnonymous, isTrue); }); @@ -51,7 +68,7 @@ void main() { () async { RequestUser? value; final handler = userProvider()( - (context) { + (_) { value = context.read(); return Response(body: ''); }, @@ -60,9 +77,11 @@ void main() { Uri.parse('http://localhost/'), headers: {'Authorization': 'bearer some token'}, ); - final context = _MockRequestContext(); when(() => context.request).thenReturn(request); + when(() => context.read()).thenReturn(RequestUser.anonymous); + await handler(context); + expect(value, equals(RequestUser.anonymous)); expect(value!.isAnonymous, isTrue); }); @@ -71,9 +90,11 @@ void main() { 'provides correct RequestUser ' 'when authorization header is valid.', () async { const userId = '__user_id__'; + final requestUser = _MockRequestUser(); + when(() => requestUser.id).thenReturn(userId); RequestUser? value; final handler = userProvider()( - (context) { + (_) { value = context.read(); return Response(body: ''); }, @@ -82,9 +103,12 @@ void main() { Uri.parse('http://localhost/'), headers: {'Authorization': 'Bearer $userId'}, ); - final context = _MockRequestContext(); + when(() => context.request).thenReturn(request); + when(() => context.read()).thenReturn(requestUser); + await handler(context); + expect(value, isA().having((r) => r.id, 'id', userId)); }); });