Javascript 将函数间谍到计算get中
我试图在计算属性中监视方法:Javascript 将函数间谍到计算get中,javascript,vuejs2,jestjs,vue-test-utils,Javascript,Vuejs2,Jestjs,Vue Test Utils,我试图在计算属性中监视方法: time() { get() { let time = this.winLimitByType(this.gameId, this.winLimitType, this.timeRangeIndex)[this.timeType]; if (time) { time = time.slice(0, 5); if (this.timeType === TO_TIME) { time = this.getR
time() {
get() {
let time = this.winLimitByType(this.gameId, this.winLimitType, this.timeRangeIndex)[this.timeType];
if (time) {
time = time.slice(0, 5);
if (this.timeType === TO_TIME) {
time = this.getRealTimeToDisplay(time);
}
}
return time;
},
set(value) {
...
}
}
运行测试时:
const wrapper = factory(WinLimitType.AMOUNT_IN_TIME_SLOTS_DAY,
OperationObjectGenerator.generateTimeRange('number', '10:00', '11:59:00'), 'toTime');
const spyGetRealTimeToDisplay = jest.spyOn(wrapper.vm, 'getRealTimeToDisplay');
wrapper.vm.time;
expect(spyGetRealTimeToDisplay).toBeCalledWith('11:59');
获取错误信息:
预期已使用以下函数调用模拟函数:
["11:59"]
但它没有被称为
有没有办法测试它
谢谢。您没有调用时间方法。()错过下午时间。应该是wrapper.vm.time()时间是一个计算属性,不缺少()。当我用()调用时,我得到了错误:“wrapper.vm.time不是函数”当我存储wrapper.vm.time的结果时(例如:const result=wrapper.vm.time),我得到了好的结果。但是spy没有附加任何内容。请尝试使用()调用。这是函数jest offer.toHaveBeenCalledWith()是toBeCalledWith()的别名。预期结果相同。