Skip to content

Instantly share code, notes, and snippets.

@gupta-shrinath
Created September 27, 2021 09:45
Show Gist options
  • Save gupta-shrinath/d782276f1e65b0fc081bc672d0d03a18 to your computer and use it in GitHub Desktop.
Save gupta-shrinath/d782276f1e65b0fc081bc672d0d03a18 to your computer and use it in GitHub Desktop.
// Function to test //
Future<List<Contact>> fetchContacts(http.Client client) async {
final response = await client.get(Uri.parse(url));
if (response.statusCode == 200) {
if (json.decode(response.body).toString().length > 2) {
List jsonResponse = json.decode(response.body);
return jsonResponse.map((e) => Contact.fromJson(e)).toList();
} else {
throw Exception('Failed to load contacts');
}
}
throw Exception('Failed to load contacts');
}
// test case//
test('throws an exception if http call returns empty list', () async {
final client = MockClient((request) async {
return Response(jsonEncode({}), 200);
});
expect(await fetchContacts(client),
throw Exception('Failed to load contacts'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment