分配给属性的匿名函数的角度测试

时间:2019-04-24 17:22:24

标签: angular unit-testing karma-jasmine

我需要测试Angular组件函数(func1)是否由在另一个angular方法内部动态实例化的对象的匿名函数属性调用。理解起来有些棘手,但让我解释一下。

我有一个导出的类,该类具有带有匿名函数作为类型的属性。像这样:

export class Detail {
  id: string;
  name: string;
  onSave: () => void;
}

在我的组件中,我有一个实例化Detail类对象并为调用onSave的{​​{1}}属性设置匿名函数的方法。

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的匿名函数内部被调用

0 个答案:

没有答案
相关问题