【问题标题】:Angular: How to test RadioButton?Angular:如何测试 RadioButton?
【发布时间】:2021-07-15 02:18:18
【问题描述】:

Angular Material 提供了 MatButtonHarness 来测试常规的 HTML 按钮。有了它,你可以'getText','click'。

有没有类似测试 RadioButtons 的东西?我希望 MatRadioHarness 存在,但它不存在。

如果没有,推荐的方法是什么?

谢谢!

【问题讨论】:

    标签: angular unit-testing testing jasmine


    【解决方案1】:

    我找不到任何文档,但查看“MatButtonHarness”的源代码(~myproject\node_modules@angular\material\button)我能够使用单选按钮的线束类识别位置(c:\Project \AngularLearning\Frontend\node_modules@angular\material\radio)。

    在“测试”子文件夹中,我能够找到与 Radio Button Harness 一起使用的实际类:MatRadioGroupHarness 和 MatRadioButtonHarness。显而易见的答案,但令人困惑的是单选按钮的类中没有“按钮”。

    在我找到特定的类后,我搜索了这些类并确定了工作示例:https://stackblitz.com/angular/rllllvapblod?file=src%2Fapp%2Fradio-harness-example.spec.ts

    让它工作的一个重要部分是将 MatRadioModule 添加到我的测试初始化​​的导入部分:

    describe('MapsComponent', () => {
      let component: MapsComponent;
      let fixture: ComponentFixture<MapsComponent>;
      let componentService: HexesService;
    
      beforeEach(
        waitForAsync(() => {
          TestBed.configureTestingModule({
            imports: [MatRadioModule],
            declarations: [MapsComponent],
          }).compileComponents();
        ...
    

    希望这也能对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2018-01-12
      • 2017-02-22
      • 2019-04-21
      • 1970-01-01
      相关资源
      最近更新 更多