Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 如何模拟HttpClientResponse以返回字符串_Dart_Flutter_Flutter Test - Fatal编程技术网

Dart 如何模拟HttpClientResponse以返回字符串

Dart 如何模拟HttpClientResponse以返回字符串,dart,flutter,flutter-test,Dart,Flutter,Flutter Test,我正在尝试在重构到dart:io.HttpClient之后编写一个测试 到目前为止,一切似乎都很顺利 var responseBody = await response.transform(utf8.decoder).join(); 以下测试抛出NoSuchMethodError:对null调用了方法“join” MockHttpClient http = new MockHttpClient(); MockHttpClientRequest request = new MockHttpCli

我正在尝试在重构到dart:io.HttpClient之后编写一个测试

到目前为止,一切似乎都很顺利

var responseBody = await response.transform(utf8.decoder).join();
以下测试抛出NoSuchMethodError:对null调用了方法“join”

MockHttpClient http = new MockHttpClient();
MockHttpClientRequest request = new MockHttpClientRequest();
MockHttpHeaders headers = new MockHttpHeaders();
MockHttpClientResponse response = new MockHttpClientResponse();
MockStream stream = new MockStream();
when(http.getUrl(Uri.parse('http://www.example.com/')))
    .thenReturn(new Future.value(request));
when(request.headers)
    .thenReturn(headers);
when(request.close())
    .thenReturn(new Future.value(response));
when(response.transform(utf8.decoder))
    .thenReturn(stream);
when(stream.join())
    .thenReturn(new Future.value('{"error": {"message": "Some error"}}'));
我确实看到了,但它使用的是http包,而不是dart:io

我也试过了,但也返回了空值


非常感谢

问题在于,在模拟流时,实际上需要实现大量不同的方法才能使其正常工作。如果您喜欢颤振回购中的示例,那么最好使用真实流。为确保身体设置正确,请使用utf8编码器

final MockHttpClientResponse response = new MockHttpClientResponse();
// encode the response body as bytes.
final List<int> body = utf8.encode('{"foo":2}');

when(response.listen(typed(any))).thenAnswer((Invocation invocation) {
  final void Function(List<int>) onData = invocation.positionalArguments[0];
  final void Function() onDone = invocation.namedArguments[#onDone];
  final void Function(Object, [StackTrace]) onError = invocation.namedArguments[#onError];
  final bool cancelOnError = invocation.namedArguments[#cancelOnError];
    return new Stream<List<int>>.fromIterable(<List<int>>[body]).listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
});
final MockHttpClientResponse=new MockHttpClientResponse();
//将响应体编码为字节。
最终列表体=utf8.encode('{“foo”:2}');
when(response.listen(键入(任意)).thenAnswer((调用){
最终void函数(List)onData=invocation.positionalArguments[0];
final void Function()onDone=invocation.namedArguments[#onDone];
最终void函数(Object,[StackTrace])onError=invocation.namedArguments[#onError];
final bool cancelOnError=invocation.namedArguments[#cancelOnError];
返回新的Stream.fromIterable([body])。侦听(onData,onDone:onDone,onError:onError,cancelOnError:cancelOnError);
});

此外,我最近发布了一个软件包,帮助您测试这些客户端,这样您就不必嘲笑:谢谢Jonah!我用你的软件包进行了测试。