我正在尝试创建一些简单的单元测试,这是代码:
import 'dart:io';
import 'package:unittest/unittest.dart';
void main() {
test('check if sent data equals expected data', () {
HttpServer.bind('127.0.0.1', 8080)
.then(expectAsync1((HttpServer server) {
server.listen((HttpRequest request) {
expect(request.runtimeType.toString(), equals('_HttpRequest'));
request.fold(new BytesBuilder(), (prev, next) {
prev.addAll(next);
return prev;
})
.then(expectAsync1((BytesBuilder bb) {
var bytes = bb.takeBytes(),
helloString = new String.fromCharCodes(bytes);
expect(helloString, equals('hello'));
request.response.write(helloString);
request.response.close();
server.close();
}));
});
}));
HttpClient client = new HttpClient();
client.get('127.0.0.1', 8080, '/')
.then((HttpClientRequest request) {
request.add('hello'.codeUnits);
return request.close();
});
});
}
给我一个错误:打破异常:'dart:async/stream_impl.dart':断言失败:第 149 行 pos 12:'!_isClosed' 不正确。
经过一些解决方法后,我发现负责的人是request.add('hello'.codeUnits)
所以现在我无法将数据添加到HttpClientRequest
. 任何解决方法?