#angular #typescript #unit-testing #jestjs #jasmine
Вопрос:
Я пытаюсь протестировать функции открытия и закрытия MatDialog. Все, что я пытаюсь, даже функция открытия не может быть протестирована или функция закрытия. Как я могу издеваться и на самом деле проверить это? Если я буду делать то же самое, что и в моем коде, я получу эту ошибку для проверки функции «закрыть»:» Аргумент типа «закрыть» не может быть присвоен параметру типа «ключ матдиалога «» TS:
//... imports etc. dialogRef:MatDialogReflt;DialogComponentgt; 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",()=gt;{ beforeEach(async () =gt; { await TestBed.configureTestingModule({ imports:[appModule,MatDialogModule], providers:[DialogComponent], }).compileComponents(); fixture=TestBed.createComponent(TestComponent); component=fixture.componentInstance; fixture.detectChanges(); }) test("should test", ()=gt;{ 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) })
Ответ №1:
Я верю, что у вас есть лишняя точка
test("should test", ()=gt;{ spyOn(component.dialogService,'open').and.callThrough(); spyOn(component.dialogService,'close').and.callThrough(); component.test(); expect(component.dialogService.open).toHaveBeenCalled(); expect(component.dialogService.close).toHaveBeenCalled(); })