我有一个具有以下 ngOnInit 函数的组件,它轮询服务方法以获取状态更新:
ngOnInit() {
this.waitingForMachineToStart = interval(2000)
.pipe(
distinctUntilChanged(),
switchMap(() => this.machineService.getMachineStatus(this.machineId)),
)
.subscribe(res => {
this.machineStatus = res.status;
if (this.machineStatus === 'STARTED') {
this.machineStarted = res
this.notify()
}
});
}
我正在尝试使用以下代码测试更新是否实际发生:
beforeEach(() => {
fixture = TestBed.createComponent(UploadComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should start checking for status updates', fakeAsync(() => {
const machineService = TestBed.get(MachineService);
spyOn(machineService, 'getMachineStatus').and.returnValue(Observable.create().pipe(map(() => {status :'STARTED'})));
// Should not be initialised yet
expect(component.machineStarted).toBeUndefined();
tick(2000);
//FAILING TEST
expect(component.machineStarted).toBe({status :'STARTED'});
}));
但是,测试仍然失败。
提前致谢