angular - 分配给属性的匿名函数的角度测试
问题描述
我需要测试一个 Angular 组件函数 (func1) 是否被另一个 Angular 方法中动态实例化的对象的匿名函数属性调用。有点难以理解,但让我解释一下。
我有一个导出的类,该类具有一个匿名函数作为类型的属性。像这样的东西:
export class Detail {
id: string;
name: string;
onSave: () => void;
}
在我的组件中,我有一个方法可以实例化 Detail 类的对象,并为onSave
调用funFn
.
myFunction(item: Item){
const detail = new Detail();
detail.onSave= (){
this.runFn(item);
}
this.someService.doSomethingWith(details);
}
runFn(item: Item){
... do something cool.
}
我想测试是否在执行runFn
时被调用onConfirm
。所以,在我的测试用例中,我有:
it("Should calls runFn", function () {
spyOn(Detail.prototype, 'onSave').and.callThrough();
const mockItem = new Item();
component.myFunction(mockItem);
expect(component.runFn).toHaveBeenCalledWith(mockItem);
});
上面的代码不起作用,因为onSave
它不是方法,而是属性。那么,我如何模拟在其中创建的实例myFunction
并测试runFn
在分配给的匿名函数内部被调用onSave
解决方案
推荐阅读
- latex - 打开 Latex Overleaf 模板:Parskip.sty 未找到?
- c# - 如何使用将静态成员方法作为参数的 [Theory] 标记进行 C# XUnit 测试?
- javascript - 即使刷新页面,如何在页面中保留附加的 html 元素
- html - 图像在第一次画廊加载时重叠
- google-sheets - 计算按列中的项目分组的日期之间的持续时间
- network-programming - 在 Fargate 工作节点上运行的 EKS 应用程序无法连接到同一子网中的 elasticache redis 集群
- reactjs - 运行速度低于 60fps 的 React 虚拟化列表。如何优化?
- android - 如何更改底部应用栏项目颜色?
- reactjs - Material UI Autocomplete Chip onDelete 不工作
- r - 循环遍历行并将列名带入 R