茉莉花测试失败:预期间谍 openQuickSubtypes 已被调用。上下文菜单测试案例

jasmine test failure: Expected spy openQuickSubtypes to have been called. Context menu test cas

我收到 jasmine 测试错误,预期间谍 openQuickSubtypes 已被调用。

我已经实现了上下文菜单。

component.html

<div class="each-shift" *ngFor="let shift of shiftsWithEmptyBoxes">
    <div class="shift-cover" [ngClass]="shiftDetails(shift)">
      <div class="requested-vertical-bar"
           [ngClass]="getShiftVerticalBarColor(shift)"></div>
      <div class="shift-left-box" #subtype (contextmenu)="openQuickSubtypes(subtype, subtypeMenu, shift); $event.preventDefault();">
        {{ showShiftOverTime(shift) }}
        {{ getShiftSubTypeLetter(shift, false) }}
        {{ showSubTypeShiftNotation(shift) }}
      </div>

component.ts

 openQuickSubtypes(origin:any,menu:any,index:number)
  {
    this.contextService.openContextMenu(origin,menu,this.viewContainerRef,{data:index})
    .subscribe(openMenu=>{
    })
  }

我的测试用例:测试用例应该右击打开

it('should right click', fakeAsync(() => {
    component.shiftsWithEmptyBoxes = [{
      "shiftId": 130374,
      "shiftType": "empty"
    }
    ];
    fixture.detectChanges();
    const menuClick = fixture.debugElement.query(By.css('.shift-left-box'));
    const spyonOpenQuickSubtypes = spyOn(component, 'openQuickSubtypes').and.callThrough();
    const event = new MouseEvent('click',
    {
    view: window,
    bubbles: true,
    cancelable: true,
    relatedTarget: document
    });  
    menuClick.nativeElement.dispatchEvent(event);
    fixture.detectChanges();
    fixture.whenStable().then(() => {
      expect(spyonOpenQuickSubtypes).toHaveBeenCalled();
    });
  }));

每当我是运行测试用例时,它都会给我以下错误:

Error: Expected spy openQuickSubtypes to have been called.

尝试这种方法,不要使用 spyOn

的 return 值

it('should right click', fakeAsync(() => {
    component.shiftsWithEmptyBoxes = [{
      "shiftId": 130374,
      "shiftType": "empty"
    }
    ];
    fixture.detectChanges();
    const menuClick = fixture.debugElement.query(By.css('.shift-left-box'));
    spyOn(component, 'openQuickSubtypes').and.callThrough();
    menuClick.triggerEventHandler("contextmenu", new  MouseEvent("contextmenu"));
    fixture.detectChanges();
    expect(component.openQuickSubtypes).toHaveBeenCalled(); 
  }));