方法未覆盖Jest中的更改事件

46qrfjad  于 2023-01-22  发布在  Jest
关注(0)|答案(1)|浏览(129)

HTML〈多选[多选选项]=“已过滤的常用模板下拉列表”表单控件名称=“常用模板”[可搜索]=“真”(搜索文本更改)=“搜索常用模板($event)”类=“多选”类=“常用模板”〉
运输系统

searchCommonTemplates(event: any) {
    this.filteredCommonTemplatesDropdown = this.dropdownSearch(event, this.commonTemplatesDropdown);
  }

spec.ts

fixture.whenStable().then(() => {
        spyOn(component, 'searchCommonTemplates');
        const select = fixture.debugElement.query(By.css('cxui-multi-select')).nativeElement;
        select.value = '';  // <-- select a new value
        select.dispatchEvent(new Event('searchTextChange'));
        fixture.detectChanges();
        expect(component.searchCommonTemplates).toHaveBeenCalled();
6xfqseft

6xfqseft1#

问题是当我们这样做时:

spyOn(component, 'searchCommonTemplates');

我们正在对方法进行存根处理,以返回undefined。我们丢失了实现细节(方法实际上并没有被调用),以便了解它何时被调用,如何被调用,被调用了多少次,等等。
为了获得两个方面的好处,您必须执行.and.callThrough();

spyOn(component, 'searchCommonTemplates').and.callThrough();

相关问题