小编Ton*_*ieh的帖子

如何为setInterval()函数编写测试用例

我有一个计时器,并假设当计数器计数到3时将执行特定的功能.

var a_interval_function = function(){
    var counter = 1;
    var interval = setInterval(function(){
        if(counter === 5){
            clearInterval(interval);
        }

        // run the function when the counter is 3
        if(counter === 3){
            a_function_should_be_runned();
        }   

        counter++;
    }, 500);

    return interval;
}
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何建立一个有效的测试用例来测试计数器以及执行函数的时间.有谁知道怎么做?类似于以下内容:

// and some test case like this
it('a timer test', function(done){
    var interval = a_interval_function();
    expect(a_function_should_be_runned.state).to.equal({
        name: 'runned',
        counter: 3,
        time: 300,
    });
});
Run Code Online (Sandbox Code Playgroud)

谢谢.

javascript mocha.js

5
推荐指数
1
解决办法
6131
查看次数

标签 统计

javascript ×1

mocha.js ×1