angular - 茉莉花测试失败:预期的间谍 openQuickSubtypes 已被调用。上下文菜单测试用例
问题描述
我收到茉莉花测试错误,预计间谍openQuickSubtypes
已被调用。
我已经实现了上下文菜单。
组件.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>
组件.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();
});
}));
每当我运行测试用例时,它都会给我以下错误:
错误:预期的间谍 openQuickSubtypes 已被调用。
解决方案
试试这种方法,不要使用的返回值spyOn
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();
}));
推荐阅读
- scala - 在 Spark 中循环 scala 列表
- c++ - 即使在删除结构元素后也保留结构类型的向量元素
- aws-lambda - AWS Cognito SAML 每次登录都会创建新用户
- go - go get 和 AzureDevOps 身份验证不起作用
- d3.js - d3.js 中的分组条形图
- plotly - 集群没有出现在我的地图上,带有 scatter Mapbox
- amazon-web-services - NodeJS AWS Lambda无服务器代理通过参数获取 - 放大
- android - Flutter:当 Timer 处于活动状态时,避免 Text Widget 重新居中
- amazon-web-services - 为什么我的 Serilog PostgreSQL 接收器在部署到 AWS Lambda 时不写入?
- primefaces - 来自单个 Bean 的 PrimeFaces 确认对话框