使用业力茉莉花angular2测试空隙

时间:2018-07-02 14:10:10

标签: angular jasmine void karma-mocha

我正在尝试用茉莉花和业力测试我的代码。

当我测试一个返回值的方法时就可以了。但是我的问题是如何测试一个空方法(不返回任何内容),例如:

public aj(a: Array<x>, p: x) {
 if (a.indexOf(p) < 0) {
   a.push(p);
  }
 }

使用此功能,我检查对象x的数组是否包含对象或否。

如果不是这种情况,我将其添加到数组中。就是这样。

我以此方式进行测试

  it('', () => {
  let component= new synthese(consoService);
   let x = [pHC,pHP]
   spyOn(component,'aj');
   expect(component.aj(x,pI)).toHaveBeenCalled();

  });

我收到此错误

Error: <toHaveBeenCalled> : Expected a spy, but got undefined.
Usage: expect(<spyObj>).toHaveBeenCalled()

有人可以帮助我吗?我尝试过,但总是会出错。

1 个答案:

答案 0 :(得分:0)

像这样更改代码:

it('', () => {
  let component = new synthese(consoService);
  const spyAj = spyOn(component, 'aj');
  let x = [pHC, pHP]; // maybe you should check this, shouldn't it be let x = ['pHC','pHP']; ?

  component.aj(x, pI); // maybe you should check this, shouldn't it be component.aj(x, 'pI'); ?

  expect(spyAj).toHaveBeenCalled();
  // also, I recommend do this test:
  // check pI is in the array now since that's what the method does, push the element if it is not in the array
  expect(x.indexOf(pI)).not.toBe(-1); // I used pI, but maybe check for 'pI' as my previous recommendations.
});