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

时间:2015-04-22 00:49:06

标签: javascript unit-testing mocha sinon bluebird

我有一个函数,它使用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。

1 个答案:

答案 0 :(得分:3)

Well, sandbox.stub(global, 'setTimeout', setImmediate); seems to do the trick. Thanks @Esailija.

Here is the working test code:

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

If anyone sees any gotchas with this approach please let me know!