angular - Angular Jest 测试异步代码不是异步的
问题描述
这是一个组件上的方法
public onClick(event: any): void {
this.isLoading = true;
this.service.getSomething().subscribe( ("foo") => {
this.isLoading = false;
})
}
然后测试
it('test', fakeAsync(()=> {
component.onClick()
expect(component.isLoading).toBe(true);
tick()
expect(component.isLoading).toBe(false);
}))
它没有说 isLoading 是假的。调试显示代码是按顺序执行的,这意味着在订阅内部的代码执行后返回控件。
解决方案
似乎Observable.of
本身不是异步的。你必须让它异步
推荐阅读
- python - 将脚本作为并行子进程运行
- apache-kafka - 卡夫卡期待地关闭。无法创建许可证主题
- visual-studio - 无法在 Visual Studio 2019 中下载 Android 模拟器
- swift - 如何减少 SwiftUI 中大量图像的内存占用
- node.js - Node.Js HTTP 响应中断
- mysql - 每周统计看到的用户
- ios - 如何在 Xcode 12 中使用 iOS 12 模拟器
- sql-server - 从更改跟踪表中读取 - 什么事务隔离级别?
- javascript - 打字稿:检查特定函数是否返回值?
- oracle - 使用动态变量插入表 PLSQL