[英]How to unit test return value of function - Angular (Jasmine/Karma)
I am wondering if there is a way to correctly test the return value of a function in Angular.我想知道是否有一种方法可以正确测试 Angular 中函数的返回值。 I want to essentially test the return value to be true for one test and write another to test the opposite scenario.
我想基本上测试一个测试的返回值是否为真,并编写另一个测试相反的场景。
Ts component: Ts 组件:
get() {
if (this.object == undefined) {
return true;
} else {
return false;
}
}
The only way I can see fit right now to test the return value is to set a variable to hold the return value.我现在认为适合测试返回值的唯一方法是设置一个变量来保存返回值。 Below is my attempt, I'm just stuck on asserting what is to be expected.
下面是我的尝试,我只是坚持断言是预期的。
Test:测试:
it('should call get function and return true', () => {
//Arrange
component.object = undefined;
//Act
component.get();
//Assert
expect(component.get). <-- *stuck here*
});
Get the value in the act and then check it to be true
in the assert .获取行为中的值,然后在assert 中检查它是否为
true
。
it('should call get function and return true', () => {
// Arrange
component.object = undefined;
// Act
var result = component.get();
// Assert
expect(result).toBe(true);
});
On a side note the body of the method get
could be simplified to just附带说明一下,
get
方法的主体可以简化为
return this.object === undefined;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.