Jasmine .toHaveBeenCalledTimes throws' undefined不是构造函数'错误

时间:2016-10-07 23:11:03

标签: jasmine karma-jasmine

我有以下单元测试:

describe('shouldDeleteLogos', () => {
    beforeEach(() => {
      productService.product.project.parts['Logo'] = {
        name: 'Logo'
      };
      productService.product.project.parts['Accent'] = {
        name: 'Accent'
      };
    });

    it(`should only call deleteLogos once if the part was provided to the method`, () => {
      let part: Part = new Part();
      part.name = 'Logo';
      spyOn(creationSpacePSTLInstance, 'deleteLogos');
      creationSpacePSTLInstance.shouldDeleteLogos(part);
      expect(creationSpacePSTLInstance.deleteLogos).toHaveBeenCalledTimes(1);
    });
});

然而,当我运行测试时,我收到以下错误:TypeError: undefined is not a constructor (evaluating 'expect(creationSpacePSTLInstance.deleteLogos).toHaveBeenCalledTimes(1)')

这里有什么问题?我已经监视了这个方法,我想测试它只被调用一次。根据文档,这应该是有效的。它仅适用于.toHaveBeenCalled();,而不适用于时间参数。

以下是我正在使用的Jasmine和Karma版本。

"jasmine-core": "2.3.4"
"karma": "0.13.22",

1 个答案:

答案 0 :(得分:2)

Jasmine 2.4中引入了

toHaveBeenCalledTimes()。如果你升级,那应该修复它。