测试Jasmine&中功能的返回值Angular2

时间:2017-11-03 12:39:41

标签: angular unit-testing jasmine karma-jasmine

在Jasmine中,您可以通过监视来检查已运行的功能;你也可以查看给出了什么参数(如果有的话);但是如何检查该函数返回的值是什么?

组件

ngOnInit() {
   this.checkString('Hello World');
}

checkString(str: string): boolean {
  if (str === 'Hello World') {
    return true;
  } else {
    return false;
  }
}

规格

...
component = fixture.componentInstance;
...


it('should check string equals `Hello World` and return boolean', () => {

  let spy = spyOn(component, "checkString");

  fixture.detectChanges(); // This runs the component OnInit() which contains a call to checkString();

  expect(spy).toBeTruthy(); // <<< This line fails. How can I get the returned value of the method?

});

0 个答案:

没有答案