如何使用运算符 && 制作 if 条件的测试用例和||角度11

发布于 2025-01-11 18:53:44 字数 867 浏览 0 评论 0原文

我正在尝试为此编写测试用例,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2025-01-18 18:53:44

抛开该测试用例是否有效,以及它是否有意义 - 您正在以错误的方式为控件分配值

//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);
  });

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

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

//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);
  });

You cannot reassign variable and expect that in every place that you have previously used that variable change will be in effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文