【发布时间】:2020-12-14 05:22:26
【问题描述】:
我有一个执行更新数据场景的 refresh() 函数,这里我想要实现的是每 1 分钟它必须在没有用户交互的情况下自动刷新,所以我在 ngoninit 中使用 setinterval 以便我可以每 60 秒执行一次,我在 ngDestroy 中清除这些间隔;
这里是component.ts
ngOnInit(): any {
this.refresh();
this.interval = setInterval(() => {
if (this.hideLog) {
this.updateWorkflow();
} else {
this.refresh();
}
}, 60000);
}
ngOnDestroy(): void {
this.clearInt();
}
clearInt(): void {
if (this.interval) {
clearInterval(this.interval);
}
}
如何在我的 spec.ts 文件中为此添加测试,以便 ngonint 测试描述应该通过
运行我的测试用例时出错:
错误:1 个周期性计时器仍在队列中。
谁能帮我解决这个问题
【问题讨论】:
-
看看
fakeAsync和flush或tick。另外我建议你让间隔在ngZone之外运行,并在必要时返回 -
检查了添加 fakeasync,flush 和 tick,仍然面临同样的问题。移动 this.interval = setInterval(() => { if (this.hideLog) { this.updateWorkflow(); } else { this.refresh(); } }, 60000);到新函数并在 ngoninit 中调用该函数,但仍然面临同样的错误
标签: javascript angular unit-testing testing jasmine