我试图为这个测试用例编写测试用例,但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);
});如何使这个测试用例正确?
发布于 2022-03-04 17:19:03
如果该测试用例是否有效,以及如果它有任何意义,则搁置一边--您正以错误的方式将值分配给控件。
//bad description of test, you are not testing if test is testing fromToHours method. Could be rather "returns true if no values are provided"
it('should test fromToHours()', () => {
expect(component.fromToHours()).toEqual(true);
component.form.controls['workingHoursTo'].setValue("17:00");
component.form.controls['workingHoursTo'].setValue(null);
expect(component.fromToHours()).toEqual(false);
component.form.controls['workingHoursTo'].setValue(null);
component.form.controls['workingHoursTo'].setValue("17:00");
expect(component.fromToHours()).toEqual(false);
});您不能重新分配变量,并期望在您以前使用过的每个地方,该变量更改都将生效。
https://stackoverflow.com/questions/71354823
复制相似问题