简体   繁体   English

期望从测试失败的另一个方法调用方法

[英]expecting method call from another method failing on test

Here is the function:这是 function:

handleAddClinic() {
    HFSClinicTableSchema.headers.map((th) =>
        th.checked ? (th.checked = false) : th
    );
    this.popTableSchema = { ...HFSClinicTableSchema, rows: [] };
    this.openDialog(this.popupTemp);
}
openDialog(templateRef: TemplateRef<HTMLAllCollection>) {
    const dialogRef = this.dialog.open(templateRef);
    dialogRef.afterClosed().subscribe((result) => {
        console.log(`Dialog result: ${result}`, HFSClinicTableSchema);
    });
}

here is the spec i do:这是我做的规范:

fit('should reset the page', fakeAsync(() => {
    spyOn(component, 'handleAddClinic');
    spyOn(component, 'openDialog');
    const addButton = fixture.debugElement.query(
        By.css('button#btnAddClinic')
    );
    addButton.nativeElement.click();
    tick();
    expect(component.handleAddClinic).toHaveBeenCalled(); //ok
    tick(5000);
    expect(component.openDialog).toHaveBeenCalled(); // error
}));

event though i given 5ms, my testing is failing at;尽管我给了 5 毫秒,但我的测试失败了;

tick(5000); expect(component.openDialog).toHaveBeenCalled();

error:错误:

 Error: Expected spy openDialog to have been called.
        at <Jasmine>

Really not able to understand the issue.真的无法理解这个问题。 any one help me to understand please?有人帮我理解吗?

  • You do not need fakeAsync and tick in this scenario在这种情况下,您不需要 fakeAsync 和 tick
  • When you spy on a method the code in that method does not get called unless you use callThrough当你监视一个方法时,除非你使用 callThrough,否则该方法中的代码不会被调用
fit('should reset the page', () => {
  spyOn(component, 'handleAddClinic').and.callThrough();
  spyOn(component, 'openDialog');
  const addButton = fixture.debugElement.query(By.css('button#btnAddClinic'));
  addButton.nativeElement.click();

  expect(component.handleAddClinic).toHaveBeenCalled();
  expect(component.openDialog).toHaveBeenCalled();
});

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

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