首页 > 解决方案 > 在 @ViewChild 变量中使用 spyOn 时出错

问题描述

我有一个使用的组件

@ViewChild(NgbDropdown) public dropdown: NgbDropdown;

this.dropdown.isOpen();

在我的规范文件中,我需要将isOpen()结果模拟为 true,但我似乎无法监视它。

收到此错误:

 Error: <spyOn> : could not find an object to spy upon for isOpen()
        Usage: spyOn(<object>, <methodName>)

提前致谢!

标签: angularjasminekarma-jasmineangular-decorator

解决方案


这与 linter 有关

这就是它应该被嘲笑的方式

component.dropdown  = jasmine.createSpyObj(['isOpen']);

(component.dropdown as SpyObj<NgbDropdown>).isOpen.and.returnValue(true);

谢谢!


推荐阅读