From 81085f4d958d0c087d38f2b5883ede275f13fa9b Mon Sep 17 00:00:00 2001 From: Jorge Santana Date: Sat, 6 Jun 2020 13:48:10 -0400 Subject: [PATCH] Testing RestClient: get success, get network exception --- lib/data/network/rest_client.dart | 9 ++- pubspec.yaml | 1 + test/data/network/rest_client_mock_test.dart | 61 ++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 test/data/network/rest_client_mock_test.dart diff --git a/lib/data/network/rest_client.dart b/lib/data/network/rest_client.dart index 8e7ddc94..22a005b1 100644 --- a/lib/data/network/rest_client.dart +++ b/lib/data/network/rest_client.dart @@ -6,13 +6,18 @@ import 'package:http/http.dart' as http; import 'exceptions/network_exceptions.dart'; class RestClient { + http.Client _client; + + RestClient({http.Client client}) { + _client = client ?? http.Client(); + } // instantiate json decoder for json serialization final JsonDecoder _decoder = JsonDecoder(); // Get:----------------------------------------------------------------------- Future get(String url) { - return http.get(url).then((http.Response response) { + return _client.get(url).then((http.Response response) { final String res = response.body; final int statusCode = response.statusCode; @@ -28,7 +33,7 @@ class RestClient { // Post:---------------------------------------------------------------------- Future post(String url, {Map headers, body, encoding}) { - return http + return _client .post(url, body: body, headers: headers, encoding: encoding) .then((http.Response response) { final String res = response.body; diff --git a/pubspec.yaml b/pubspec.yaml index ee56973e..3c23fe5c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -85,6 +85,7 @@ dev_dependencies: url: https://github.com/google/inject.dart.git path: package/inject_generator ref: 7c3cbf6 + mockito: ^4.1.1 flutter_icons: image_path: "assets/icons/ic_launcher.png" diff --git a/test/data/network/rest_client_mock_test.dart b/test/data/network/rest_client_mock_test.dart new file mode 100644 index 00000000..75aad5c3 --- /dev/null +++ b/test/data/network/rest_client_mock_test.dart @@ -0,0 +1,61 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:http/http.dart' as http; + +import '../../../lib/data/network/exceptions/network_exceptions.dart'; +import '../../../lib/data/network/rest_client.dart'; + +// Create a MockClient using the Mock class provided by the Mockito package. +// Create new instances of this class in each test. +class MockClient extends Mock implements http.Client {} + +main() { + group('RestClient', () { + group('get', () { + test('returns a Map if the http call completes successfully', () async { + // Arrange + final mockitoClient = MockClient(); + final url = 'https://example.com'; + + // Act + // Use Mockito to return a successful response when it calls the + // provided http.Client. + when(mockitoClient.get(url)) + .thenAnswer((_) async => http.Response('{"name": "Jorge"}', 200)); + + final restClient = RestClient(client: mockitoClient); + final response = await restClient.get(url); + + // Assert + // response should be a map + if (!(response is Map)) fail('response should be a map'); + }); + + test( + 'throws a network exception if the http call completes with an error', + () async { + // Arrange + final mockitoClient = MockClient(); + final url = 'https://example.com'; + dynamic error; + + // Act + // Use Mockito to return an unsuccessful response when it calls the + // provided http.Client. + when(mockitoClient.get(url)) + .thenAnswer((_) async => http.Response('Not Found', 404)); + + final restClient = RestClient(client: mockitoClient); + try { + await restClient.get(url); + } catch (e) { + error = e; + } + + // Assert + if (!(error is NetworkException)) + fail("didn't throw network exception"); + }); + }); + }); +}