Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating test cases for RestClient #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/data/network/rest_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<dynamic> 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;

Expand All @@ -28,7 +33,7 @@ class RestClient {

// Post:----------------------------------------------------------------------
Future<dynamic> 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;
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
61 changes: 61 additions & 0 deletions test/data/network/rest_client_mock_test.dart
Original file line number Diff line number Diff line change
@@ -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");
});
});
});
}