Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Sinon spy callCount属性在某些测试中返回0_Javascript_Unit Testing_Sinon - Fatal编程技术网

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中做了异步操作吗?