1- import 'dart:convert' ;
21import 'dart:io' ;
32
43import 'package:dart_frog/dart_frog.dart' ;
@@ -12,7 +11,8 @@ import '../../../../../routes/api/v1/countries/index.dart' as route;
1211// --- Mocks ---
1312class _MockRequestContext extends Mock implements RequestContext {}
1413
15- class _MockHttpRequest extends Mock implements Request {} // Use dart_frog Request
14+ class _MockHttpRequest extends Mock
15+ implements Request {} // Use dart_frog Request
1616
1717class _MockHtCountriesClient extends Mock implements HtCountriesClient {}
1818// --- End Mocks ---
@@ -98,7 +98,7 @@ void main() {
9898 ).called (1 );
9999 });
100100
101- test ('uses max limit when limit parameter exceeds max' , () async {
101+ test ('uses max limit when limit parameter exceeds max' , () async {
102102 final countries = createSampleCountries (5 );
103103 // Limit is 150, should be clamped to 100 (defined in route)
104104 uri = Uri .parse ('http://localhost/api/v1/countries?limit=150' );
@@ -148,20 +148,25 @@ void main() {
148148 equals ('Invalid query parameter: "limit" must be an integer.' ),
149149 );
150150 verifyNever (
151- () => mockClient.fetchCountries (limit: any (named: 'limit' ), startAfterId: any (named: 'startAfterId' )),
151+ () => mockClient.fetchCountries (
152+ limit: any (named: 'limit' ),
153+ startAfterId: any (named: 'startAfterId' ),
154+ ),
152155 );
153156 });
154157
155- // Add test for client throwing CountryFetchFailure (handled by global handler)
158+ // Add test for client throwing CountryFetchFailure
159+ //(handled by global handler)
156160 test ('lets CountryFetchFailure bubble up (handled globally)' , () async {
157161 uri = Uri .parse ('http://localhost/api/v1/countries?limit=10' );
158162 when (() => request.uri).thenReturn (uri);
159- final exception = CountryFetchFailure ('DB error' );
163+ const exception = CountryFetchFailure ('DB error' );
160164 when (
161165 () => mockClient.fetchCountries (limit: 10 , startAfterId: null ),
162166 ).thenThrow (exception);
163167
164- // Expect the specific exception to be thrown, letting the global handler catch it
168+ // Expect the specific exception to be thrown, letting the global handler
169+ // catch it
165170 expect (
166171 () => route.onRequest (context),
167172 throwsA (isA <CountryFetchFailure >()),
@@ -183,7 +188,6 @@ void main() {
183188 // Remove 'id' as it's generated by client/constructor usually
184189 final requestJson = Map <String , dynamic >.from (newCountryJson)..remove ('id' );
185190
186-
187191 setUp (() {
188192 // Set request method for all POST tests in this group
189193 when (() => request.method).thenReturn (HttpMethod .post);
@@ -215,7 +219,7 @@ void main() {
215219 test ('returns 400 Bad Request for invalid JSON body' , () async {
216220 // Override request.json stub to throw format exception
217221 when (() => request.json ()).thenThrow (
218- FormatException ('Unexpected character' ),
222+ const FormatException ('Unexpected character' ),
219223 );
220224
221225 final response = await route.onRequest (context);
@@ -228,10 +232,11 @@ void main() {
228232 verifyNever (() => mockClient.createCountry (any ()));
229233 });
230234
231- test ('returns 400 Bad Request for invalid country data structure' , () async {
232- // Provide JSON missing a required field
233- final invalidJson = {'iso_code' : 'DE' , 'flag_url' : '...' }; // Missing name
234- when (() => request.json ()).thenAnswer ((_) async => invalidJson);
235+ test ('returns 400 Bad Request for invalid country data structure' ,
236+ () async {
237+ // Provide JSON missing a required field
238+ final invalidJson = {'iso_code' : 'DE' , 'flag_url' : '...' }; // Missing name
239+ when (() => request.json ()).thenAnswer ((_) async => invalidJson);
235240
236241 final response = await route.onRequest (context);
237242
@@ -243,9 +248,10 @@ void main() {
243248 verifyNever (() => mockClient.createCountry (any ()));
244249 });
245250
246- // Add test for client throwing CountryCreateFailure (handled by global handler)
247- test ('lets CountryCreateFailure bubble up (handled globally)' , () async {
248- final exception = CountryCreateFailure ('Duplicate entry' );
251+ // Add test for client throwing CountryCreateFailure
252+ // (handled by global handler)
253+ test ('lets CountryCreateFailure bubble up (handled globally)' , () async {
254+ const exception = CountryCreateFailure ('Duplicate entry' );
249255 when (() => mockClient.createCountry (any ())).thenThrow (exception);
250256
251257 // Expect the specific exception to be thrown
@@ -258,7 +264,7 @@ void main() {
258264 });
259265 });
260266
261- test ('returns 405 Method Not Allowed for unsupported methods' , () async {
267+ test ('returns 405 Method Not Allowed for unsupported methods' , () async {
262268 // Test with PUT, DELETE etc.
263269 when (() => request.method).thenReturn (HttpMethod .put);
264270 final responsePut = await route.onRequest (context);
@@ -268,7 +274,12 @@ void main() {
268274 final responseDelete = await route.onRequest (context);
269275 expect (responseDelete.statusCode, equals (HttpStatus .methodNotAllowed));
270276
271- verifyNever (() => mockClient.fetchCountries (limit: any (named: 'limit' ), startAfterId: any (named: 'startAfterId' )));
272- verifyNever (() => mockClient.createCountry (any ()));
277+ verifyNever (
278+ () => mockClient.fetchCountries (
279+ limit: any (named: 'limit' ),
280+ startAfterId: any (named: 'startAfterId' ),
281+ ),
282+ );
283+ verifyNever (() => mockClient.createCountry (any ()));
273284 });
274285}
0 commit comments