我正在尝试在https://flutter.io/networking/重构为 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。
我也试过https://github.com/flutter/flutter/blob/master/dev/manual_tests/test/mock_image_http.dart但这也返回一个空值。
非常感谢提前!