我正在开发angular2网络应用。 我想测试我的功能,它是在一段时间后重新连接服务器连接。看起来像这样(不是实际代码):
reconnect() {
setTimeout(()=>{
this.connection.stop()
this.cnnect(); // Logic here
}, this.connectionTimeout);
this.connectionTimeout+=this.connectionTimoutDeley;
}
它通过主题来解决它正在控制connectionStatus,如果它设置为Disconected im手动重新启动它到结束时间。
如果超时和deley相继发生,我想对它进行单元测试。但它的asynhrounous,我不知道如何测试它.... 我的洗礼不起作用:
it('should deley reconnect process', fakeAsync (() => {
//Arrange
var timeSpanForFirstReconnect;
var timeSpanForSecondReconnect;
var actDone = false;
service.setimeouts(1000, 100000, 100);
//service.getConnectionStatus().subscribe(status => {
// //Assert
// if (actDone) {
// expect(timeSpanForSecondReconnect).toBeGreaterThan(timeSpanForFirstReconnect);
// done();
// }
//});
//Act
timeSpanForFirstReconnect = performance.now();
statusSubject.next(new ConnectionStatus(ConnectionStatuses.Disconected));
tick();
timeSpanForFirstReconnect = performance.now() - timeSpanForFirstReconnect;
timeSpanForSecondReconnect = performance.now();
statusSubject.next(new ConnectionStatus(ConnectionStatuses.Disconected));
tick();
timeSpanForSecondReconnect = performance.now() - timeSpanForSecondReconnect;
console.log(timeSpanForFirstReconnect + ' ------ ' + timeSpanForSecondReconnect);
expect(timeSpanForSecondReconnect).toBeGreaterThan(timeSpanForFirstReconnect);
actDone = true;
}));