angular - 使用 Mat 对话框在 Angular 中单元测试失败
问题描述
我们在 UI 中有一个按钮,单击该按钮在内部调用一个方法,该方法调用服务以执行某些操作,然后使用 mat 对话框显示返回消息。单元测试失败说“open() 方法不存在”。错误在代码行中被抛出
spyOn(component.dialog, 'open').and.callThrough();
在点击按钮后被调用:
public dosomething(){
this.service
.dosomething()
.subscribe((result) => {
this.showConfirmDialog(result);
this.isDisabled = true;
},
error => {
this.showErrorDialog();
});
}
以下是 showConfirmDialog() 的代码
private showConfirmDialog(result){
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = {
messageHeader: heading,
answerDescription: result,
isShownAnswerDescription: true,
isHidden: true,
} as MessageData;
const dialogRef = this.dialog.open(MessageDialogComponent, dialogConfig);
}
以下是单元测试:
it('should do something on click of button button', () => {
spyOn(TestBed.inject(ConfigurationManagementService), 'dosomething')
.and.returnValue(of('message'));
spyOn(component.dialog, 'open').and.callThrough();
component.isDisabled = false;
component.dosomething();
expect(component.dialog.open).toHaveBeenCalledWith(MessageDialogComponent);
});
如果我遗漏任何东西,请告诉我...
提前致谢!!!
解决方案
推荐阅读
- docker - 如何将文件复制到 docker 映像?
- python - Python Reduce:参数顺序导致错误
- php - file_get_contents(https://www.google.com/recaptcha/api/siteverify):无法打开流:在 laravel 项目中
- sql - SQL:年份为零的最小日期
- api - 是否可以通过 API 在创建日期之前删除 Nexus 存储库上的 docker 图像?
- javascript - 为什么 module.export 设置多次?
- dependencies - 使用 Yarn 2 使依赖项的依赖项可用
- xamarin - 如何检查用户是否有权查看 Xamarin.Forms 中的页面
- node.js - 安装 ursa 时出错 - 错误 C2660:'v8::String::Utf8Length':函数不接受 0 个参数
- c# - 抛出异常以回滚并同时重定向到另一个动作