Angular2-在单元测试中打开和关闭mdDialog
我目前正在尝试在我的一个单元测试中打开和关闭一个angular material 2对话框。开始看起来不错,但我发现一个函数应该在结束后调用,但它从来没有被调用,我认为这是因为对话框没有按我希望的那样关闭 我要测试的代码:Angular2-在单元测试中打开和关闭mdDialog,angular,unit-testing,typescript,angular-material2,Angular,Unit Testing,Typescript,Angular Material2,我目前正在尝试在我的一个单元测试中打开和关闭一个angular material 2对话框。开始看起来不错,但我发现一个函数应该在结束后调用,但它从来没有被调用,我认为这是因为对话框没有按我希望的那样关闭 我要测试的代码: openDialog() { this.dialogRef = this.dialog.open( ConfirmationDialogComponent, { height: '210px', width: '335px', di
openDialog() {
this.dialogRef = this.dialog.open( ConfirmationDialogComponent, {
height: '210px',
width: '335px',
disableClose: true
});
this.dialogRef.componentInstance.title = 'Delete Selected Intents?';
this.dialogRef.componentInstance.content = 'All corresponding data will be deleted';
this.dialogRef.componentInstance.option1 = 'Cancel';
this.dialogRef.componentInstance.option2 = 'Delete';
this.dialogRef.afterClosed().subscribe( result => {
this.afterDialogClose(result);
});
}
到目前为止,我的测试(由于afterDialogClose未按预期调用而失败):
有人能告诉我我做错了什么,以及如何强制关闭对话框并调用afterDialogClose()函数吗?谢谢 我认为主要的错误是 队列中还有1个计时器 另见
jasmine.done
而不是async/fakeAsync
it('should call afterDialogClose when the dialog closing event triggered', (done) => {
spyOn(component, 'afterDialogClose');
component.openDialog();
fixture.detectChanges();
fixture.whenStable().then(() => {
component.dialog.closeAll();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.afterDialogClose).toHaveBeenCalled();
done();
});
});
});
您遇到了哪个错误?@yurzui“预期spy afterDialogClose已被调用。”并且
2个计时器仍在队列中。
?@yurzui 1个计时器仍在队列中,是的,正确修复了队列错误。但是,afterDialogClose函数仍然没有按预期调用,这意味着对话框没有关闭。您可以在我的plunker中复制它吗?
it('should call afterDialogClose when the dialog closing event triggered', (done) => {
spyOn(component, 'afterDialogClose');
component.openDialog();
fixture.detectChanges();
fixture.whenStable().then(() => {
component.dialog.closeAll();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.afterDialogClose).toHaveBeenCalled();
done();
});
});
});