【发布时间】:2020-09-26 23:38:25
【问题描述】:
我正在尝试测试一个有角度的材质对话框,具体来说,我想测试它是否在对话框关闭时修改了路线。为此,我不想模拟/存根对话框(我不认为?),因为我希望它运行执行重新路由的实际 .subscribe 方法。
在我的实际对话框中出现两个错误:
this.dialogRef.afterClosed is not a function
login-dialog-component.spec.ts
describe('LoginDialogComponent', () => {
let component: LoginDialogComponent;
let fixture: ComponentFixture<LoginDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [LoginDialogComponent],
imports: [
RouterTestingModule.withRoutes([]),
MatDialogModule
],
providers: [
{provide: MatDialog},
{provide: MAT_DIALOG_DATA, useValue: {}},
{provide: MatDialogRef, useValue: {}},
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我遇到的第二个错误是打开我的对话框的组件的测试。它报告:
NullInjectorError: R3InjectorError(DynamicTestModule)[MatDialog -> Overlay -> Overlay]:
NullInjectorError: No provider for Overlay!
打开我的对话框的组件测试:login.component.spec.ts
describe('LoginComponent', () => {
let component: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
providers: [
{ provide: MatDialog },
{ provide: MAT_DIALOG_DATA, useValue: {} },
{ provide: MatDialogRef, useValue: {}}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
LoginComponent 本身并不真正需要任何测试,但如上所述,它给了我“No provider for overlay”,大概是因为构造函数是:
constructor(
private dialog: MatDialog
) { }
LoginComponent 然后打开 LoginDialog。
我希望在 LoginDialog 中测试的代码是:
ngOnInit(): void {
this.dialogRef.afterClosed().subscribe(_ =>
this.router.navigate(['', {outlets: {login: null}}])
);
}
我想确保当对话框关闭时路由器真正导航离开。
所以我的问题是
- 如何将真实的 dialogRef 传递给 LoginDialog 的构造函数?
- 如何摆脱 No provider for Overlay 错误?
顺便说一句,如果我改变了
{ provide: MatDialogRef, useValue {})
到:
{ provide: MatDialogRef }
(所以我大概是在尝试传递一个实际的 MatDialogRef)
错误变化自
this.dialogRef.afterClosed is not a function
到:
Can't resolve all parameters for MatDialogRef: (?, ?).
谢谢。
【问题讨论】: