unit-testing - Angular 6 - 单元测试 Mat-Select
问题描述
1:mat-select 有 4 个值,1,2,3,4。
下面的代码适用于选择。所以如果对读者有帮助,我想分享一下。
it('check the length of drop down', async () => {
const trigger = fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
trigger.click();
fixture.detectChanges();
await fixture.whenStable().then(() => {
const inquiryOptions = fixture.debugElement.queryAll(By.css('.mat-option-text'));
expect(inquiryOptions.length).toEqual(4);
});
});
2:我需要另一个测试来验证同一个 mat-select 中的默认值是否为 3。当页面加载时,下拉菜单的默认值设置为 3。
it('should validate the drop down value if it is set by default', async () => {
const trigger = fixture.debugElement.query(By.css('.mat-select-trigger')).nativeElement;
trigger.click();
fixture.detectChanges();
await fixture.whenStable().then(() => {
const inquiryOptions = fixture.debugElement.queryAll(By.css('.mat-option-text'));
const value = trigger.options[0].value;
expect(value).toContain(3);
});
});
任何帮助表示赞赏。
解决方案
这个在 Angular 7 中为我工作
const debugElement = fixture.debugElement;
// open options dialog
const matSelect = debugElement.query(By.css('.mat-select-trigger')).nativeElement;
matSelect.click();
fixture.detectChanges();
// select the first option (use queryAll if you want to chose an option)
const matOption = debugElement.query(By.css('.mat-option')).nativeElement;
matOption.click();
fixture.detectChanges();
fixture.whenStable().then( () => {
const inputElement: HTMLElement = debugElement.query(By.css('.ask-input')).nativeElement;
expect(inputElement.innerHTML.length).toBeGreaterThan(0);
});
推荐阅读
- c - C 评分程序:评分程序不返回“C”
- python - 我们可以像 11.3 版本中的执行命令活动一样通过 datastage 序列阶段编写 python 脚本吗?
- c# - c# n-tier解决方案web服务客户端到服务层的奇怪行为
- google-apps-script - GmailApp.SendEmail() 有时会发送两封电子邮件
- excel - Excel VBA循环不移动到下一行
- typescript - Typescript Generic 类和 Object.assign 看不到值
- c# - 如何访问和修改作为字典值给出的对象的属性?
- excel - 如何根据公式中的excel中的两个标准对列进行排序?
- python - 将范围更改为调用环境
- django - 如何让人们在我的网站上下载文件?