angular - 如何期望用 Jasmine 调用多个方法
问题描述
有这个代码
ngOnInit() {
this.loadFilesList(this.massiveLoadModuleConfig.CONSTANTS.FILES_LIST_REFRESH_INTERVAL);
}
private loadFilesList(interval: number): void {
this.massiveLoadService.loadMassiveLoadFilesList().subscribe(() => this.getFilesList());
this.timeout = setTimeout(() => {
this.loadFilesList(interval);
}, interval);
}
我怎么能期望在 Jasmine 中多次调用方法“massiveLoadService.loadMassiveLoadFilesList()”?
谢谢
解决方案
- 使用
fakeAsync
+tick
- 我们只想测试 loadFilesList() 是否在 amount 时间后再次调用
it ('...', fakeAsync(() => {
const spy = spyOn(massiveLoadService, 'loadFilesList').and.callThrough();
fixture.detectChanges(); // detect change againt
tick(); // invoke async
tick(yourInterval);
fixture.detectChanges(); // detect change againt
fixture.destroy(); // kill the interval, otherwise it will continue running = error
expect(spy).toHaveBeenCalledTimes(2);
})
注意:我不会像这样使用嵌套函数 + setInterval 来重复一个函数,我会使用timer()
from rxjs 以获得更好的实现
推荐阅读
- c# - 调试 C# 外部类
- c - 如何在C中获得pulseaudio音量?
- python - Whatsapp 消息自动回复
- python - 将 XML 转换为 JSON 或 Avro(在 python 中)
- uwp - 如何调试 nutc_driver.exe 错误:使用 Net Native 工具链在发布模式下构建 UWP 应用程序时出现 ILT0005
- python - tensorflow 2.0 中的 Batch Normalization 使预测结果更差
- python - python中列表的性质,为什么我会得到一个重复列表?
- sql - 将 2 个表与条件合并
- ssl - 使用 SubjectAltName 'OtherName" 在 openSSL 中创建 CSR 时出错
- php - if 语句为真,即使它不是保存票时