我想知道是否有一种方法可以正确测试 Angular 中函数的返回值。我想本质上测试一个测试的返回值是否正确,并编写另一个测试来测试相反的场景。
Ts 组件:
get() {
if (this.object == undefined) {
return true;
} else {
return false;
}
}
我现在认为测试返回值合适的唯一方法是设置一个变量来保存返回值。以下是我的尝试,我只是坚持断言预期的内容。
Test:
it('should call get function and return true', () => {
//Arrange
component.object = undefined;
//Act
component.get();
//Assert
expect(component.get). <-- *stuck here*
});
获取其中的值act然后检查它是true
in the assert.
it('should call get function and return true', () => {
// Arrange
component.object = undefined;
// Act
var result = component.get();
// Assert
expect(result).toBe(true);
});
另外请注意方法的主体get
可以简化为
return this.object === undefined;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)