【发布时间】:2019-03-08 19:14:00
【问题描述】:
我有一个带有以下 ngOnInit 函数的组件,它轮询服务方法以获取状态更新:
ngOnInit() {
interval(2000).pipe(
switchMap(() => this.dataService.getStatus())
).subscribe((result) => {
this.uploadStatus = result;
);
}
我正在尝试使用以下代码测试更新是否实际发生:
beforeEach(() => {
fixture = TestBed.createComponent(UploadComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should start checking for status updates', fakeAsync(() => {
const dataService = TestBed.get(DataService);
// Mock the getStatus function
spyOn(dataService, 'getStatus').and.returnValue(Observable.create().pipe(map(() => 'woo')));
// Should not be initialised yet
expect(component.uploadStatus).toBeUndefined();
tick(2000);
expect(component.uploadStatus).toBe('woo');
}));
但是component.uploadStatus 始终为空。我应该如何去测试这种类型的场景?理想情况下,我想随着时间的推移检查多个更新。
谢谢
【问题讨论】:
标签: angular unit-testing jasmine rxjs