繁体   English   中英

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

[英]Test void with karma jasmine angular2

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

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

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()

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

像这样更改代码:

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.
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM