【发布时间】:2019-06-24 10:00:49
【问题描述】:
代码:
var processFooBar = function (message, callback) {
doFooAndBar(message, callback);
};
module.exports.processFooBar = processFooBar;
var doFooAndBar = function (data, callback) {
async.parallel(
[
function (callback) {
foo(data, function (err, response) {
callback(err, response);
});
},
function (callback) {
bar(data, function (err, response){
callback(err, response);
});
}
],
function (err, results) {
callback(err, results);
}
);
};
module.exports.doFooBar = doFooBar;
单元测试
describe('Process data', function () {
var fooStub;
beforeEach(function (done) {
done();
});
afterEach(function (done) {
fooStub.restore();
done();
});
it('can process data', function (done) {
fooStub = sinon.stub(fileName, 'foo').yields(null, null);
barNockCall();
app.processFooBar(message,
function (err, response) {
nock.isDone().should.be.true;
nock.cleanAll();
done();
}
}
});
我收到以下错误:
can process data:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
(/Path/To/Test.js)
如果我在 async.parallel 中删除 foo(),则不会收到错误消息。另外,我猜第一个 sinon.stub 是 fooStubis 没有被调用。
【问题讨论】:
-
这段代码与minimal reproducible example相差甚远,尤其是在完整性方面。
-
使用 RunKit 使其可重现。我维护诗乃,经常用它来重现案例。这就像在浏览器中编写 Node 代码一样。示例:runkit.com/fatso83/5bbfafa21ebc47001398e7de
标签: node.js sinon nock sinon-chai