【发布时间】:2021-07-15 02:18:18
【问题描述】:
Angular Material 提供了 MatButtonHarness 来测试常规的 HTML 按钮。有了它,你可以'getText','click'。
有没有类似测试 RadioButtons 的东西?我希望 MatRadioHarness 存在,但它不存在。
如果没有,推荐的方法是什么?
谢谢!
【问题讨论】:
标签: angular unit-testing testing jasmine
Angular Material 提供了 MatButtonHarness 来测试常规的 HTML 按钮。有了它,你可以'getText','click'。
有没有类似测试 RadioButtons 的东西?我希望 MatRadioHarness 存在,但它不存在。
如果没有,推荐的方法是什么?
谢谢!
【问题讨论】:
标签: angular unit-testing testing jasmine
我找不到任何文档,但查看“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();
...
希望这也能对某人有所帮助!
【讨论】: