Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter unitTest:Invalid argument (string): Contains invalid characters

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/

like image 698
Cyrus the Great Avatar asked Apr 21 '20 13:04

Cyrus the Great


1 Answers

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',
  }
)
like image 195
VarunBarad Avatar answered Nov 12 '22 21:11

VarunBarad