Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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 js中测试集合间隔_Javascript_Mocha.js_Sinon_Sinon Chai - Fatal编程技术网

Javascript 如何在sinon js中测试集合间隔

Javascript 如何在sinon js中测试集合间隔,javascript,mocha.js,sinon,sinon-chai,Javascript,Mocha.js,Sinon,Sinon Chai,我正试图为setInterval()编写一个单元测试,但我不确定如何监视fetchState() maincode.js: var pollStatus = function(interval, killPolling) { // Clear Interval if function is called again if (killPolling || StatusPollObj) { clearInterval(StatusPollObj); St

我正试图为
setInterval()
编写一个单元测试,但我不确定如何监视
fetchState()

maincode.js:

var pollStatus = function(interval, killPolling) {
   // Clear Interval if function is called again 
   if (killPolling || StatusPollObj) {
        clearInterval(StatusPollObj);
        StatusPollObj = false;
    }

    // Call once before setInterval Starts
    fetchState();
    StatusPollObj = setInterval(function() {
        if(somecondtion_to_check_inactivity) return;
        fetchState();
    }, interval);
};
规格js

 it("state.json setInterval Call",function() {
    this.clock = sinon.useFakeTimers();
    var helper = new state.HELPER();
    var spy = sinon.spy(helper, "fetchState");

    helper.pollStatus('80000', false);
    expect(spy.called).to.be.true;
    this.clock.tick(80000);
    expect(spy.called).to.be.true;
});

间谍未注册到setInterval。函数fetchState应作为参数传递给函数

var someFun = function(callFunc, interval, killPolling) {
    callFunc();
    StatusPollObj = setInterval(function() {
        if(somecondtion_to_check_inactivity) return;
        callFunc();
    }, interval);
} 
你的测试应该是这样的

it("state.json setInterval Call",function() {
    this.clock = sinon.useFakeTimers();
    var helper = new state.HELPER();
    var mySpy = sinon.spy(helper, "fetchState");

    helper.pollStatus(mySpy,'80000', false);
    expect(mySpy.called).to.be.true;
    this.clock.tick(80000);
    expect(mySpy.called).to.be.true;
});