我试图通过模拟套接字来测试使用WebSockets的类
我的班级看起来像
class WebLoggerHandler{
WebLoggerHandler(){
var webSocket = new WebSocket('hostUrl');
webSocket.onOpen.first.then((_) {
webSocket.sendString('hello');
});
}
}
我正在嘲笑WebSocket类,如下所示
class MockWebSocket extends Mock implements WebSocket{
Stream<Event> onOpen;
}
我测试的主体看起来像
var webSocket = new MockWebSocket();
//fire the onOpen event
webSocket.onOpen = new Stream.fromFuture( new Future.value( new Event( "onOpen")));
WebLoggerHandler underTest = new WebLoggerHandler();
//test that the hello message was sent when the socket was opened
webSocket.getLogs( callsTo("sendString", 'hello')).verify( happenedExactly(1))
但是在检测到webSocket日志发生后,onClose事件发生时它无效。
如何使代码等待Future发生?
由于
答案 0 :(得分:1)
阅读API文档并进行了解决。测试需要返回未来
test("it should send 'hello' to the server", () {
return new Future.value().then((_) {
webSocket.getLogs(callsTo("sendString", {'hello'})).verify(happenedExactly(1));
});
});