Angular:如何测试RadioButton


Angular Material提供了MatButtonHarness来测试常规HTML按钮。有了它,你可以"获取文本","点击"。

有类似于测试单选按钮的东西吗?我希望MatRadioHarness存在,但事实并非如此。

如果没有,建议采用什么方法?

谢谢!

我找不到任何文档,但正在查找"MatButtonHarness";(~myproject\node_modules@angular\材料\按钮(我能够用单选按钮的线束类识别位置(c:\Project\AngularLearning\Frontend\node_modules@angular\材料\无线电(。

在"测试"子文件夹中,我能够找到使用单选按钮线束的实际类:MatRadioGroupHarness和MatRadioButtonHarness。答案显而易见,但令人困惑的是,单选按钮的类中没有"按钮"。

在我找到特定的类后,我在谷歌上搜索了这些类,并确定了工作示例:https://stackblitz.com/angular/rllllvapblod?file=src%2Fapp%2Fradio-线束-样品-规格-

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

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

希望这也能帮助到别人!

最新更新