gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:24:48 24 4
gpt4 key购买 nike

我有一个使用 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'
});
});
});
});
});
});

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

关于javascript - 如何在测试中伪造 Bluebird 的计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com