Created
September 27, 2021 09:45
-
-
Save gupta-shrinath/d782276f1e65b0fc081bc672d0d03a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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