【问题标题】:How to test a checkbox is checked in angular unit test如何在角度单元测试中检查复选框
【发布时间】:2020-02-14 11:12:39
【问题描述】:

您好,我在 CSS 的帮助下使用复选框来伪装成切换按钮。当复选框为真时,该功能很简单,它启用切换按钮,反之亦然。我想在我的单元测试中检查一下。我知道我在下面的代码中遗漏了一些东西。任何帮助将不胜感激

测试

it('should toggle switch to on if user response equals true', fakeAsync(() => {
    spectator.get(service).user.and.returnValue({});
    spyOn(spectator.get(serivce2), 'method').and.returnValue({Payload: {success: true}});
    tick();
    spectator.detectChanges();
    expect(spectator.query('.toggle-button').getAttribute('checked')).toBeTruthy();
}));

【问题讨论】:

    标签: angular unit-testing karma-jasmine angular8


    【解决方案1】:

    假设.toggle-button<input type="checkbox"> 上。您可以使用标准 HTML DOM API spectator.query('.toggle-button').checked

    【讨论】:

      【解决方案2】:

      通过获取 nativeElement

      试试这个
      expect(spectator.query('.toggle-button').nativeElement.getAttribute('checked')).toBeTruthy();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2020-07-10
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        相关资源
        最近更新 更多