【问题标题】:How to write unit test case to Reset the radio button values using angular如何编写单元测试用例以使用角度重置单选按钮值
【发布时间】:2020-06-27 10:10:00
【问题描述】:

我正在尝试编写一个单元测试用例来清除单选按钮的检查值

通过单击删除图标链接,它应该清除值

HTML 文件

 <mat-icon  class="remove-icon" (click)="resetradioValues('gender')">delete</mat-icon>

ts 文件

 resetradioValues(name: string){
    this.form.get(name).patchValue(null);
  }

我已经为上述代码编写了一个单元测试用例,但它不适合我

it('should clear radio button values', () => {
    const param = Object.assign({},radio, { name: 'test' });
    console.log(param);
    component.resetradioValues(param.name);

    });

请告诉我任何人都可以解决这个问题

【问题讨论】:

    标签: angular jasmine karma-jasmine


    【解决方案1】:

    我们将通过实际单击图标并查看会发生什么来进行集成测试。

    试试:

    it('should clear radio button values', () => {
          // arrangement
          const matIconElement = fixture.debugElement.query(By.css('mat-icon.remove-icon')).nativeElement;
          // click
          matIconElement.click();
          // assertions, you can assert how you like
          expect(component.form.get('test').value).toBe(null);
        });
    // ========== Edit (Unit test) ==============
    it('should clear radio button values', () => {
          // arrangement
          component.resetradioValues('test);
          // assertions, you can assert how you like
          expect(component.form.get('test').value).toBe(null);
        });
    

    【讨论】:

    • 感谢您的回复。我想在单元测试用例中使用 resetradioValues 函数。
    • 我收到错误 TypeError: Cannot read property 'forEach' of null
    • 在你的组件中,我敢打赌有一个forEach 并且该变量为空。确保在单元测试中实例化它。
    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多