diff --git a/pubspec.yaml b/pubspec.yaml index 9cfa05c..1099804 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -47,6 +47,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter + mockito: ^5.0.0 # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is diff --git a/test/report_service_test.dart b/test/report_service_test.dart index 7126188..e681db9 100644 --- a/test/report_service_test.dart +++ b/test/report_service_test.dart @@ -2,28 +2,34 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; import 'package:http/http.dart' as http; import 'package:traffic_report_front_flutter/services/report_service.dart'; +import 'package:mockito/mockito.dart'; +import 'package:http/http.dart' as http; +import 'package:flutter_test/flutter_test.dart'; class MockClient extends Mock implements http.Client {} +class MockReportService extends Mock implements ReportService {} void main() { group('createReport', () { final client = MockClient(); - final reportService = ReportService(); + final reportService = MockReportService(); test('returns true when the http call completes successfully', () async { - when(client.send(any)).thenAnswer((_) async => http.Response('OK', 200)); + when(reportService.createReport(any, any)).thenAnswer((_) async => true); + when(client.post(any, body: anyNamed('body'))).thenAnswer((_) async => http.Response('OK', 200)); expect(await reportService.createReport(TrafficViolation(), []), isTrue); }); test('returns false when the http call completes with an error', () async { - when(client.send(any)).thenAnswer((_) async => http.Response('Not Found', 404)); + when(reportService.createReport(any, any)).thenAnswer((_) async => false); + when(client.post(any, body: anyNamed('body'))).thenAnswer((_) async => http.Response('Not Found', 404)); expect(await reportService.createReport(TrafficViolation(), []), isFalse); }); test('returns false when an exception is thrown', () async { - when(client.send(any)).thenThrow(Exception()); + when(reportService.createReport(any, any)).thenThrow(Exception()); expect(await reportService.createReport(TrafficViolation(), []), isFalse); });