我有这样的简单课
abstract class ServerConnection {
Stream onMessage;
Future send(Message m);
...
}
您使用消息调用发送,异步响应将流式传输到 onMessage。现在我需要通过这个连接发送各种消息和测试响应。我做了这个测试,但它不工作(我迷失了这些expectAsynces)并且_waitForNextEvent不是很好(但至少工作!)
test('connection', () {
getCon().then((ServerConnection c) {
Future<Message> _waitForNextEvent(Stream s) {
StreamSubscription ss;
Completer c = new Completer();
ss = s.listen((Message m) {
ss.cancel();
c.complete(m);
});
return c.future;
}
_waitForNextEvent(c.onMessage).then(expectAsync1(
(m) => expect(m, new isInstanceOf<OkMessage>('OkMessage'))
));
c.send(new StatusMessage());
_waitForNextEvent(c.onMessage).then(expectAsync1(
(m) => expect(m, new isInstanceOf<PongMessage>('PongMessage'))
));
c.send(new PingMessage());
});
});
那么测试 Dart Streams 的正确和优雅的方法是什么?