繁体   English   中英

Angular 如何测试 MatDialog 函数 - 打开和关闭

[英]Angular how to test MatDialog functions - open and close

我正在尝试测试 MatDialog 打开和关闭功能。 我正在尝试的任何东西,即使是 open 函数也无法测试,也无法测试 close 函数。 我如何模拟并实际测试它? 如果我在我的代码中这样做,我将在 'close' 函数上为 spyOn 收到此错误:“类型为 ''close'' 的参数不可分配给类型为 'keyof MatDialog'”的参数 TS:

//... imports etc.
dialogRef:MatDialogRef<DialogComponent>

constructor(public dialogService:MatDialog){}

public test(){
this.openProgressDialog();
//http request
this.closeProgressDialog();
}

public openProgressDialog(){
this.dialogRef=this.dialogService.open(DialogComponent,{
  disableClose:true,
  height:'150px',
  width:'400px'
});
}

public closeProgressDialog(){
  this.dialogRef.close();
}

规格:

//... imports
describe("TestComponent",()=>{
   beforeEach(async () => {
   await TestBed.configureTestingModule({
   imports:[appModule,MatDialogModule],
   providers:[DialogComponent],
}).compileComponents();
 
  fixture=TestBed.createComponent(TestComponent);
  component=fixture.componentInstance;
  fixture.detectChanges();
})

test("should test", ()=>{
  spyOn(component.dialogService,'open').and.call.Through(); // the problem is at this two lines
  spyOn(component.dialogService,'close').and.call.Through(); 
  component.test();
  //expect ... (just for example)
})

我相信你有一个额外的点

test("should test", ()=>{
  spyOn(component.dialogService,'open').and.callThrough(); 
  spyOn(component.dialogService,'close').and.callThrough();
 
  component.test();
  
  expect(component.dialogService.open).toHaveBeenCalled();
  expect(component.dialogService.close).toHaveBeenCalled();
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM