angular - 如何测试 Angular Material 对话框是否打开
问题描述
我正在编写一个单元测试来检查函数调用是否打开了 Material 对话框元素。我得到了 False,而预期的结果是 True。组件功能为:
x.component.ts
openCartDialog(): void {
this.dialog.open(CartComponent);
this.isCartDialogOpen = true;
}
我的 x.component.spec.ts 文件有以下代码和测试:
export class DialogMock {
open(): void {
return;
}
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MainPageComponent, CartComponent, ConfirmButtonComponent],
imports: [RouterTestingModule, HttpClientModule, MatTabsModule, MatDialogModule],
providers: [
{ provider: MatDialog, useClass: DialogMock },
],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MainPageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should open the cart dialog', () => {
component.openCartDialog();
expect(component.isCartDialogOpen).toBeTrue();
});
我认为问题出在我的 beforeEach() 中 CartComponent 没有正确实例化。Cart 组件在其模板中也使用了其他组件...我在这段代码中只包含了 ConfirmButtonComponent,但还有更多。
谢谢你的帮助!
解决方案
推荐阅读
- cytoscape - 将表格导入 cytoskape,导入标题但不导入数据
- android - 运行时的 Gradle 构建错误(Realm DB、android、kotlin)
- java - 如何防止 Spring 上下文关闭,直到集群 Quartz 作业未完成?[ 弹簧 + 石英 ]
- angular - 如何在formArray中我的垫子输入的占位符中设置不同的文本?
- node.js - Nodejs express multer文件上传+路径包含双斜杠
- docker - 如何在 docker 中运行 qt-gui?
- tsql - SQL Server Merge Statement编译抛出错误关键字WHEN附近的语法不正确
- powershell - 右键单击 ps1 文件 > 编辑:不再与 ISE 关联(卸载 Sapien Powershell studio 后)
- azure - 如何从本地终端提取 Azure 订阅 ID
- android - 如何从点击的号码中获取电话号码(在其他应用程序上)并将其显示在我自己的应用程序中