angular - 被调用但未在角度测试中显示的功能
问题描述
我有这个代码。
export class MyComponent implements OnInit {
ngOnInit() {
// some code
this.method1(); // It comes up to here.
}
method1() {
console.log('method1'); // It is not showing this.
// Other method calls
}
}
我的规格文件是:
it('should call method1', fakeAsync(() => {
const element = fixture.debugElement.nativeElement;
spyOn(comp, 'method1');
fixture.detectChanges();
// some code
fixture.detectChanges();
fixture.whenStable().then(async() => {
expect(comp.method1).toHaveBeenCalled(); // this works
});
}));
这里有什么问题?
解决方案
当您监视一个方法时,您正在用假的方法替换原始方法,这就是没有调用 method1 的原因
如果你想监视一个方法而不替换它,你必须使用 callThrough:
spyOn(comp, 'method').and.callThrough()
推荐阅读
- angular - Angular 13 多入口点代码覆盖率
- c# - 无法从给定的 mod 和 exp 生成公钥。抛出“坏数据”
- visual-studio - Visual Studio 2019 - 快速查找?(CTRL-T) 像 reshaper
- android - 在哪里放置 Android 应用程序所需的 ONNX 运算符
- excel - 如何获取最新的电子邮件并附加到现有文件,而不是使用 VBA 遍历所有项目?
- c# - 如何根据 C# 中的配置更改 com 接口?
- woocommerce - 创建一个简码以显示结帐时显示的 Woocommerce 统一费率价格
- c# - 网络文件路径和直接文件路径之间的区别?
- angular - 了解 Rxjs 可观察对象
- php - Wordpress:使用分页时重复的帖子