根据角度文档https://angular.io/guide/testing-components-scenarios#async-使用fakeasync 进行测试
使用CCD_ 1、CCD_ 2和CCD_。我创建了一个测试,在我看来,它是文档中呈现的代码的对应物(没有组件(。不幸的是它失败了。你能解释一下原因吗?
it('how to test observables. Example #5', fakeAsync(() => {
let progress = true;
// of(['somevalemiitter']) //without asapScheduler, it will fail
throwError('bleblebl') //without asapScheduler, it will fail
.pipe(finalize(() => {
progress = false;
// done();
}))
.subscribe(console.log, err => console.log(err));
expect(progress).toBeTruthy();
console.log('before tick');
tick();
console.log('after tick');
expect(progress).toBeFalsy();
}));
产出;
'bleblebl'
'before tick'
'after tick'
我希望它是
'before tick'
'bleblebl'
'after tick'
只是因为使用了CCD_ 4。如果我使用asapScheduler
,那么它可以按预期工作。
这似乎只是throwError
的指定行为,订阅是同步处理的,因此您的测试或fakeAsync
没有任何问题。
例如,Angular组件中的这种方法:
handleClick() {
throwError("test").subscribe(console.log, console.log);
console.log("test2");
}
这导致:
test
test2