如何使用运算符 && 制作 if 条件的测试用例和||角度11
我正在尝试为此编写测试用例,但 npm 测试失败:
这是我在 ts 文件中的函数:
fromToHours(){
let from = this.form.controls.workingHoursFrom.value;
let to = this.form.controls.workingHoursTo.value;
if((from == "" && to != "") || (from != "" && to == "")){
return false;
}
else{
return true;
}
}
这是我的 spec.ts 文件,该测试用例 npm 测试失败:
it('should test fromToHours()', () => {
expect(component.fromToHours()).toEqual(true);
let from = "";
let to = "";
component.form.controls['workingHoursTo'].setValue(from);
component.form.controls['workingHoursTo'].setValue(to);
from = "17:00";
to = null;
expect(component.fromToHours()).toEqual(false);
from = null;
to = "17:00";
expect(component.fromToHours()).toEqual(false);
});
如何使此测试用例正确?
I am trying to write test case for this one and it is failing for npm test:
This is my function in ts file:
fromToHours(){
let from = this.form.controls.workingHoursFrom.value;
let to = this.form.controls.workingHoursTo.value;
if((from == "" && to != "") || (from != "" && to == "")){
return false;
}
else{
return true;
}
}
This is my spec.ts file and this test case is failing for npm test:
it('should test fromToHours()', () => {
expect(component.fromToHours()).toEqual(true);
let from = "";
let to = "";
component.form.controls['workingHoursTo'].setValue(from);
component.form.controls['workingHoursTo'].setValue(to);
from = "17:00";
to = null;
expect(component.fromToHours()).toEqual(false);
from = null;
to = "17:00";
expect(component.fromToHours()).toEqual(false);
});
How to make this test case right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抛开该测试用例是否有效,以及它是否有意义 - 您正在以错误的方式为控件分配值
您不能重新分配变量并期望在您之前使用过的每个地方该变量更改都会生效。
Putting aside if that test case is valid or not, and if it makes any sense - you are assigning values to the controls the wrong way
You cannot reassign variable and expect that in every place that you have previously used that variable change will be in effect.