繁体   English   中英

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

[英]How to write a test case for a setInterval() function

我有一个计时器,并假设当计数器计数到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;
}

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

// 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,
    });
});

谢谢。

也许您可以使用sinon.useFakeTimers()

例如:

var sinon  = require('sinon');
var expect = require('chai').expect;

var a_function_should_be_runned = sinon.spy();

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;
}

describe('timer tests', function() {

  before(function() {
    this.clock = sinon.useFakeTimers();
  });

  after(function() {
    this.clock.restore();
  });

  it('a timer test', function() {
    var interval = a_interval_function();

    // At time 0, we don't expect the function to have been called.
    expect(a_function_should_be_runned.called).to.be.false;

    // Advance clock 500ms.
    this.clock.tick(500);
    expect(a_function_should_be_runned.called).to.be.false;

    // Advance clock again (1s since start)
    this.clock.tick(500);
    expect(a_function_should_be_runned.called).to.be.false;

    // Advance clock again (1.5s since start). This should
    // trigger the call to `a_function_should_be_runned`.
    this.clock.tick(500);
    expect(a_function_should_be_runned.called).to.be.true;
  });
});

这是我的天真做法:

it('a timer test', function(done){
    var interval = a_interval_function();
    setTimeout(function () {
        expect(a_function_should_be_runned.state).to.equal({
            name: 'runned',
            counter: 3,
            time: 300,
        });
        done();
    }, 3.5 * 500);
});

您在这里所做的基本上是等待1.75秒,并期望a_function_should_be_runned函数已在超时时执行。 然后,调用expect函数以检查您的断言。 最后,您调用done() 请注意,在setTimeout回调函数中调用了done() 如果不调用done()测试只会超时。

检查代码/函数是否被执行的最简单方法是仅通过输入alert或console.log ,因此您可以直观地确认正在发生的事情。 您也可以在间隔内console.log计数器,以便看到计数器上升。

屋子里的语法眼镜。

[编辑]

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 && testFunction()){
            a_function_should_be_runned();
        }   

        counter++;
    }, 500);

    return interval;
}

function testFunction(){
     if(KeyboardNinja == 'awesome'){
         return true;
     } else {
         return false;
     }


}

您的测试将永远不会失败:P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM