我正在编写一个单元测试,以检查函数调用是否打开了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没有正确实例化。购物车组件还在模板中使用其他组件...在此代码中我仅包含ConfirmButtonComponent,但还有更多。
感谢您的帮助!