我的组件如下所示,在Init中,我从服务获取初始数据,然后每15秒调用一次相同的函数来更新该数据:
networkGraph: NetworkGraphComponent;
hideSpinner: boolean = false;
ngAfterViewInit() {
this.getTopology();
}
getTopology() {
clearTimeout(this.timeoutHandler);
this.topologyService.getTopology().subscribe((topology: any) => {
...do stuff...
if (topology) {
setTimeout(()=>{ // hack to fix ngIf
this.hideSpinner = true;
});
this.networkGraph.topology = topology;
this.timeoutHandle = setTimeout(function(){
this.getTopology();
}, 15000);
}
});
我应该如何编写单元测试来测试这一点?它总是说我有计时器在排队。我读过这个
https://medium.com/@golbie/angular-testing-async-stuff-in-the-fakedasync-zone-vs-providing-custom-schedulers-27a7f83c7774
并尝试了其中的内容,但我猜我做得不对,因为我仍然得到了相同的结果。
it('should create the component', fakeAsync(()=>{
tick(20000)
fixture.detectChanges();
fixture.whenStable().then(()=>{
expect(component).toBeTruthy();
})
}));