【问题标题】:How to get the number of invocations of void function?如何获取void函数的调用次数?
【发布时间】:2018-05-14 13:41:48
【问题描述】:

我正在尝试编写一个测试来计算ngAfterViewInit() 函数的调用总数。 我正在测试我的指令,它只有一个 ngAfterViewInit() 函数。

it('myDirective is working', () => {    
  expect(myDirective.calls.count()).toEqual(2);
});

在测试的最后一行,我试图在ngAfterViewInit()函数上调用calls属性,但是出现了错误:

类型'() => void'上不存在属性调用。

【问题讨论】:

  • 你试过expect(myDirective.ngAfterViewInit).toHaveBeenCalledTimes(2) 吗?

标签: javascript angular jasmine


【解决方案1】:

您可以使用以下代码检查 ngAfterViewInit 是否被调用了两次:

expect(myDirective.ngAfterViewInit).toHaveBeenCalledTimes(2);

调用spyOn(myDirective, 'ngAfterViewInit') 后,myDirective.ngAfterViewInit 上的calls 属性可用。它不适用于myDirective 本身。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多