在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?
});