首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用操作符&和角11中的取舍来制作if条件的测试用例

如何用操作符&和角11中的取舍来制作if条件的测试用例
EN

Stack Overflow用户
提问于 2022-03-04 17:06:50
回答 1查看 995关注 0票数 0

我试图为这个测试用例编写测试用例,但npm测试失败了:

这是我在ts文件中的功能:

代码语言:javascript
复制
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测试失败了:

代码语言:javascript
复制
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);
  });

如何使这个测试用例正确?

EN

回答 1

Stack Overflow用户

发布于 2022-03-04 17:19:03

如果该测试用例是否有效,以及如果它有任何意义,则搁置一边--您正以错误的方式将值分配给控件。

代码语言:javascript
复制
//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);
  });

您不能重新分配变量,并期望在您以前使用过的每个地方,该变量更改都将生效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71354823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档