angular - Angular Jasmine 测试:无法测试材料表按钮的单击事件
问题描述
我正在角度测试文件中编写代码来测试材料对话框按钮的单击。当我运行我的测试时,我收到错误无法读取 null 的属性 'triggerEventHandler'
const setupComponent = () => {
fixture = TestBed.createComponent(CountryComponent);
component = fixture.componentInstance;
fixture.detectChanges();
};
beforeEach(() => {
mockXYZApiServiceApiService = new Mock<XYZApiService>({
getCountries: () => of(countryGridDataModel)
});
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, MatDialogModule ],
declarations: [CountryComponent],
providers: [
{
provide: XYZApiServiceApiService,
useFactory: () => mockXYZApiServiceApiService.Object,
}
],
})
.compileComponents();
});
fit('should call the mat dialog open On View click', fakeAsync(async () => {
setupComponent();
fixture.debugElement
.query(By.css('#btnViewCountry'))
.triggerEventHandler('click', {});
await fixture.whenStable();
expect(component.dialog.open).toHaveBeenCalled();
}));
解决方案
要调试,请确保它#btnViewCountry
存在且未被*ngIf
.
fit('should call the mat dialog open On View click', fakeAsync(async () => {
setupComponent();
// add this log to output the whole HTML and ensure that
// #btnViewCountry is present
console.log(fixture.nativeElement);
fixture.debugElement
.query(By.css('#btnViewCountry'))
.triggerEventHandler('click', {});
await fixture.whenStable();
expect(component.dialog.open).toHaveBeenCalled();
}));
推荐阅读
- javascript - TypeError:无法读取未定义的属性“绑定”,我不知道这意味着什么。反应js
- asp.net-core - ABP 和每个层次结构的表
- python - music21 :给定 midi 输入,输出正确拼写的音高和八度数
- google-apps-script - 为什么 Google 表格中的正则表达式公式会阻止 Google 表格脚本正常运行?
- flutter - 错误:找不到“Flutter/Flutter.h”文件#import
- node.js - 我的代码正在使用 mongoose 5.10.6,但不适用于最新版本
- sql-server - 导入的 SQL Server DB 上的日期格式已更改
- r - ggplot - 一个图上的双折线图和堆积条形图
- components - VUEX - 从 Getter 在组件中创建新对象
- ssl - SSL 证书有效但浏览器显示无效