如何在测试中伪造 Bluebird 的计时器?

How to fake Bluebird's timers in tests?

我有一个函数,它使用 Bluebird 的 Promise.delay() 方法每 5 秒递归地检查一个长 运行 任务的状态:

waitForLongRunningTask: function (taskId) {
  return checkTaskStatus(taskId)
  .then(function (result) {
    if (result.status == 'success') {
      return Promise.resolve(result);
    }
    if (result.status == 'failure') {
      return Promise.reject(result);
    }
    return Promise.delay(5000)
    .then(function () {
      return waitForLongRunningTask(taskId);
    });
  });
}

如何让被测试的函数立即完成而不是等待真正的持续时间?

目前我正在我的测试设置中这样做,它有效,但看起来很笨拙,并且不能与其他 Bluebird 计时器方法一起使用。

// beforeEach
this.sandbox = sinon.sandbox.create();
this.sandbox.stub(Promise, 'delay', Promise.resolve);

// afterEach
this.sandbox.restore();

是否有更好的方法,例如使用 Sinon 的 useFakeTimers()?当我尝试这样做时,测试会在 20 秒后超时。

我正在使用 bluebird 2.9.24、mocha 1.21.5、sinon 1.14.1 和 node 0.10.38。

好吧,sandbox.stub(global, 'setTimeout', setImmediate); 似乎可以解决问题。谢谢@Esailija。

这是工作测试代码:

var chai = require('chai');
chai.use(require('sinon-chai'));
var expect = chai.expect;
var sinon = require('sinon');
var Promise = require('bluebird');

var TestService = {

  waitForLongRunningTask: function (taskId) {
    return TestService.checkTaskStatus(taskId)
    .then(function (result) {
      if (result.status == 'success') {
        return Promise.resolve(result);
      }
      if (result.status == 'failure') {
        return Promise.reject(result);
      }
      return Promise.delay(5000)
      .then(function () {
        return TestService.waitForLongRunningTask(taskId);
      });
    });
  },

  checkTaskStatus: function () {}
};

describe('waitForLongRunningTask', function () {
  var promise, checkTaskStatus, taskId, sandbox, waitForLongRunningTask;

  function setUp () {
    taskId = 12345;
    sandbox = sinon.sandbox.create();
    waitForLongRunningTask = sandbox.spy(TestService, 'waitForLongRunningTask');
    checkTaskStatus = sandbox.stub(TestService, 'checkTaskStatus');
    sandbox.stub(global, 'setTimeout', setImmediate);
  }

  describe('when the task eventually succeeds', function () {

    beforeEach(function () {
      setUp();
      checkTaskStatus.onCall(0).returns(Promise.resolve({
        id: taskId,
        status: 'in_progress'
      }));
      checkTaskStatus.onCall(1).returns(Promise.resolve({
        id: taskId,
        status: 'in_progress'
      }));
      checkTaskStatus.returns(Promise.resolve({
        id: taskId,
        status: 'success'
      }));
    });

    afterEach(function () {
      sandbox.restore();
    });

    it('should wait for the task to succeed and then resolve with the task results', function () {

      return Promise.try(function () {

        promise = TestService.waitForLongRunningTask(taskId);

        return promise.finally(function () {
          expect(promise.isFulfilled()).to.be.true;
          expect(waitForLongRunningTask).to.have.been.calledThrice;
          expect(checkTaskStatus).to.have.been.calledThrice;
          expect(promise.value()).to.deep.equal({
            id: taskId,
            status: 'success'
          });
        });
      });
    });
  });

  describe('when the task eventually fails', function () {

    beforeEach(function () {
      setUp();
      checkTaskStatus.onCall(0).returns(Promise.resolve({
        id: taskId,
        status: 'in_progress'
      }));
      checkTaskStatus.onCall(1).returns(Promise.resolve({
        id: taskId,
        status: 'in_progress'
      }));
      checkTaskStatus.returns(Promise.resolve({
        id: taskId,
        status: 'failure'
      }));
    });

    afterEach(function () {
      sandbox.restore();
    });

    it('should wait for the task to fail and then reject with the task results', function () {

      return Promise.try(function () {

        promise = TestService.waitForLongRunningTask(taskId);

        return promise
        .error(function () {})
        .finally(function () {
          expect(promise.isRejected()).to.be.true;
          expect(waitForLongRunningTask).to.have.been.calledThrice;
          expect(checkTaskStatus).to.have.been.calledThrice;
          expect(promise.reason()).to.deep.equal({
            id: taskId,
            status: 'failure'
          });
        });
      });
    });
  });
});

如果有人发现这种方法有任何问题,请告诉我!