Skip to content

Commit

Permalink
Merge pull request #26 from yihong1120/sweep/add-report-service-tests
Browse files Browse the repository at this point in the history
Add unit tests for logging logic in ReportService
  • Loading branch information
yihong1120 authored Dec 30, 2023
2 parents 5a2aed4 + cc36d3b commit 5248112
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions test/report_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 5248112

Please sign in to comment.