flutter unitTest:无效参数(字符串):包含无效字符



我正在尝试在flutter上对http客户端进行单元测试。在嘲笑http和我的存储库类之后:

void main() {
MockHttpCLient mockHttpCLient;
FoursquareRepositoryImpl foursquareRepositoryImpl;
setUp(() {
mockHttpCLient = MockHttpCLient();
foursquareRepositoryImpl = FoursquareRepositoryImpl(client: mockHttpCLient);
});

我正在运行这个测试:

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'},
));
},
);

这是foursquareRepositoryImpl.getVenuesDetails实现:

@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));
}

但是测试失败了,我得到了这个错误:https://paste.ubuntu.com/p/Ppy3ZhnyHB/

此错误很可能是由非英文字符引起的。Dart的json.decode方法默认使用拉丁文编码器(https://stackoverflow.com/a/52993623)。

如果要从venues_details.json返回响应,请将以下标头添加到该响应中。

(_) async => http.Response(
fixture('venues_details.json'),
200,
headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8',
}
)

相关内容

最新更新