Javascript Sinon spy callCount属性在某些测试中返回0
这是我的代码:Javascript Sinon spy callCount属性在某些测试中返回0,javascript,unit-testing,sinon,Javascript,Unit Testing,Sinon,这是我的代码: //SUT.spec.js 从“/myModule”导入*作为myModule; 描述('我的问题',()=>{ 设myFuncSpy=sinon.spy(myModule,'myFunc'); 在每个之前(()=>{ myFuncSpy.reset(); }); 它(‘我的案例A’,()=>{ SUT.methodA(); expect(myFuncSpy.callCount).to.equal(1);//失败,表示为0 }); 它(‘我的案例B’,()=>{ SUT.meth
//SUT.spec.js
从“/myModule”导入*作为myModule;
描述('我的问题',()=>{
设myFuncSpy=sinon.spy(myModule,'myFunc');
在每个之前(()=>{
myFuncSpy.reset();
});
它(‘我的案例A’,()=>{
SUT.methodA();
expect(myFuncSpy.callCount).to.equal(1);//失败,表示为0
});
它(‘我的案例B’,()=>{
SUT.methodB();
expect(myFuncSpy.callCount).to.equal(1);//通过
});
});
在我的模块中,两个方法都调用了myFunc
,但仅在methodA
上未注册:
//SUT.js
从“/myModule”导入{myFunc};
导出函数methodA(){
myFunc(……);
console.log(myFunc.callCount);//Mocha输出显示1
};
导出函数methodB(){
myFunc(……);
console.log('method B ran');//Mocha输出显示这一行
console.log(myFunc.callCount);//Mocha输出显示1
};
基本上,对间谍的称呼没有明显区别。我很困惑什么可能是错的
我在SUT中添加了console.log
语句,只是为了确保spy设置正确(否则它将没有名为callCount
的属性)。另外,如果我注释掉.reset()
调用,log语句将显示undefined
,而不是1
或其他数字
这里怎么了?这当然是实际SUT的简化版本。但是,
console.log
语句表明,问题肯定不是没有执行行。在断言之前,您必须等待异步方法。您在方法A中做了异步操作吗?