【问题标题】:Angular Karma Jasmine Date Comparision角业力茉莉花日期比较
【发布时间】:2020-05-26 06:55:19
【问题描述】:

这里是我比较日期的 SelectEndDate 函数。


selectEndDate = () => {
    const currentDate = this.createDate();
    const endDate = this.addSurveyForm.value.endDate;
    const startDate = this.addSurveyForm.value.startDate;
    Eif (startDate === undefined || startDate === null || startDate === '') {
      this.addSurveyForm.patchValue({
        endDate: new FormControl({ value: ' ', disabled: true }, Validators.required)
      });
    } else {
      if (new Date(endDate) < new Date(currentDate)) {
        this.addSurveyForm.patchValue({
          endDate: ''
        });
      }
      if (new Date(endDate) < new Date(startDate)) {
        this.addSurveyForm.patchValue({
          endDate: ''
        });
      }
    }
  }

我想为此功能编写测试用例。要检查开始日期是否为空,如果是,那么我写了这个案例

it('should call selectEndDate if startDate is null ', () => {
    const startDate = '';
    component.selectEndDate();
    fixture.detectChanges();
    expect(startDate).toBe('');
  });

现在我不知道如何涵盖其他部分,我无法使用小于运算符来表示日期。 所以请帮帮我。

【问题讨论】:

标签: angular karma-jasmine date-comparison


【解决方案1】:

在这个测试中

it('should call selectEndDate if startDate is null ', () => {
    const startDate = '';
    component.selectEndDate();
    fixture.detectChanges();
    expect(startDate).toBe('');
  });

两条线

    component.selectEndDate();
    fixture.detectChanges();

不影响测试结果。 const startDate = '';selectEndDate 内部的 startDate 无关。您需要改为更改表单的值并测试表单的值。

it('should call selectEndDate if startDate is null ', () => {
    const component = fixture.componentInstance;
    component.addSurveyForm.patchValue({
      startDate: ''
    });

    fixture.detectChanges();

    component.selectEndDate();
    expect(component.addSurveyForm.value.endDate).toBe('');
  });

此外,订阅component.addSurveyForm.controls.startDate.valueChanged 并在该订阅中更新endDate 会更可靠。这样endDate 将在startDate 时更新。即使它发生在selectEndDate 函数之外

【讨论】:

    猜你喜欢
    • 2014-12-12
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2020-03-27
    • 2016-08-03
    相关资源
    最近更新 更多