1
我試圖測試通過嘲諷插座飛鏢嘲諷的Stream
我的類使用的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))
但它不工作,因爲onClose事件發生在檢查webSocket日誌之後。
如何讓代碼等待未來發生?
謝謝