我正在使用模态服务来打开、确认和关闭对话框,我正在编写它的单元测试文件,但在 Angular 上出现了错误,以下是代码。
所以,使用那段代码时出现了错误。
你能帮我如何让这个成功吗?非常感谢帮助。
modal.service.ts
@Injectable()
export class ModalService {
constructor(private dialog: MatDialog) { }
public open<modalType>(modalComponent: ComponentType<modalType>): Observable<any> {
let dialogRef: MatDialogRef<any>;
dialogRef = this.dialog.open(modalComponent, {
maxWidth: '100vw'
});
console.log(dialogRef)
dialogRef.componentInstance.body = body;
return dialogRef.afterClosed().pipe(map(result => console.log('test'); );
}
}
modal.service.spec.ts
export class TestComponent {}
describe('ModalService', () => {
let modalService: ModalService;
const mockDialogRef = {
open: jasmine.createSpy('open')
};
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ MatDialogModule ],
providers: [
ModalService,
MatDialogRef,
{ provide: MatDialog, useClass: MatDialogStub }
]
}).compileComponents();
modalService = TestBed.get(ModalService);
}));
it('open modal', () => {
modalService.open(DummyComponent, '300px');
expect(modalService.open).toHaveBeenCalled();
});
});
所以,使用那段代码时出现了错误。
TypeError: Cannot read property 'componentInstance' of undefined
你能帮我如何让这个成功吗?非常感谢帮助。