使用 setTimeout 测试 IIFE

Testing IIFE with setTimeout

测试使用 setTimeout 递归调用自身的 IIFE(立即调用函数表达式)的最佳方法是什么:

(function myFuncToBeTested() {
  // Code to be tested 
  ...
  setTimeout(myFuncToBeTested, timeout) // timeout should be checked
})()

我发现以下解决方案将全局 setTimeout 函数替换为 own 存根。这有以下问题:

// Saving original setTimeout. This should be restored in test cleanup
originalSetTimeout = global.setTimeout

// Replace  with function
global.setTimeout = function setImmediate(myFunc, interval) {
   // FIXME: This function now always called

   // Save interval to be tested
   savedInterval = interval
}

这个函数可以做成对象吗?

var myObject = (function(){

    function start(){
        myFuncToBeTested();       
        setTimeout(start, 10);
        return this;
    }

    function myFunctToBeTested(){
        //Code to be tested
    }

    return {
        start: start,
        myFuncToBeTested: myFuncToBeTested
    }
})().start();

然后您可以使用您选择的测试框架来测试:

assert( myObject.myFuncToBeTested() == expectedValue );

我想在 thedarklord47 的答案和你的存根实验之间提出一个混合解决方案 setTimeout。像您这样的 IIFE 本身就很难测试,因为您没有留下任何方法来检查它是否已被调用。您可以按如下方式修改您的API:

var repeater = {
  start: function () {
    this.func();

    setTimeout(this.start.bind(this), timeout);
  },
  func: function () {
    // code to be tested
  }
};

然后你的测试看起来像这样(因为你用 标记我已经使用过它,特别是它的假计时器 API 可以让你检查你的间隔功能):

// setup
var clock = sinon.useFakeTimers();
var spy = sinon.spy(repeater, 'func');

// test
repeater.start();
assert(spy.calledOnce);

// advance clock to trigger timeout
clock.tick(timeout);
assert(spy.calledTwice);

// advance clock again
clock.tick(timeout);
assert(spy.calledThrice);

// teardown
clock.restore();
spy.restore();