I am trying to unit test http client on flutter. After mocked http and my repository class :
void main() {
MockHttpCLient mockHttpCLient;
FoursquareRepositoryImpl foursquareRepositoryImpl;
setUp(() {
mockHttpCLient = MockHttpCLient();
foursquareRepositoryImpl = FoursquareRepositoryImpl(client: mockHttpCLient);
});
I am running this test:
test(
'should perform a get request on a url with application/json header',
() async {
//arrange
when(mockHttpCLient.get(any, headers: anyNamed('headers'))).thenAnswer(
(_) async => http.Response(fixture('venues_details.json'), 200));
//act
foursquareRepositoryImpl.getVenuesDetails(venueId);
//assert
verify(mockHttpCLient.get(
'https://api.foursquare.com/v2/venues/$venueId?client_id={{client_id}}&client_secret={{client_secret}}&v={{v}}',
headers: {'Content-Type': 'application/json; charset=utf-8'},
));
},
);
This is foursquareRepositoryImpl.getVenuesDetails
implementation :
@override
Future<VenuesDetails> getVenuesDetails(String venueId) async {
await client.get(
'https://api.foursquare.com/v2/venues/$venueId?client_id={{client_id}}&client_secret={{client_secret}}&v={{v}}',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}).timeout(Duration(seconds: 10));
}
But test is failded and i got this error: https://paste.ubuntu.com/p/Ppy3ZhnyHB/
This error is most probably caused due to non-English characters. Dart's json.decode
method uses Latin encoder by default (https://stackoverflow.com/a/52993623).
Where you are returning response from venues_details.json
, add the following header to that response.
(_) async => http.Response(
fixture('venues_details.json'),
200,
headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8',
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With