angular - 为什么使用 `fakeAsync` 测试不会使测试代码以异步方式运行?
问题描述
根据角度文档https://angular.io/guide/testing-components-scenarios#async-test-with-fakeasync
使用Observable
fakeAsync
并且tick()
应该允许测试异步实体。我已经创建了测试,在我看来,它与文档中提供的代码(没有组件)相对应。不幸的是它失败了。你能解释一下为什么吗?
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'
只是因为使用fakeAsync
. 如果我使用asapScheduler
它,它会按预期工作。
解决方案
这似乎是 的指定行为throwError
,订阅是同步处理的 - 所以您的测试或fakeAsync
.
例如,Angular 组件中的这个方法:
handleClick() {
throwError("test").subscribe(console.log, console.log);
console.log("test2");
}
这导致:
test
test2
推荐阅读
- angular - Ag 网格:如何添加具有排序顺序的行
- python - 带有签名的python包装函数
- android - 我的片段中有 webview,当滑动 webview 时, viewPager 也开始滑动
- android - “创建时不得附加 ViewHolder 视图”- 带有 ArrayList 的 RecyclerView
- c++ - 分析程序执行的一部分
- angular - WebView 插件未安装 Ionic 4 应用程序,即使我已安装它
- javascript - Svg在鼠标拖动时缩放旋转的元素
- google-bigquery - 如何使用 Data Studio 的日期参数查询特定范围的 Firebase 分析表?
- javascript - 运行 querySelectorAll() 时 NodeList 始终为空
- mysql - 提取的 json 列上的 MySQL 全文搜索