【发布时间】:2015-11-27 05:23:30
【问题描述】:
我在下面有一个 restify 操作代码块:
function retriveAll(req, res, next) {
db.user
.find({where: {id: 1})
.then(function(user){
res.send(user);
})
.catch(function(details){
res.send(details.message);
})
.finally(function(){
next();
});
}
我想测试这个动作,专门验证 res.send() 是在这个代码块中调用的。稍后验证 res.send() 返回的数据。我正在使用 SinonJs 和 Mocha 来测试框架。这是上述方法的示例测试代码块。
describe('retrieveAll()', function() {
reqStub = {};
resStub = {send: sinon.stub()};
nextStub = sinon.stub();
beforeEach(function() {
module.retrieveAll(reqStub, resStub, nextStub);
});
// this doesn't work
// and the sub.calledCount is 0
// i wonder if it's because the res.send() is inside a Promise code block???
// if I move the res.send() out from Promise, just before next(), then it works
it('should call res.send()', function() {
sinon.assert.calledOnce(resStub.send);
});
// this one works
it('should call next', function() {
sinon.assert.calledOnce(nextStub);
});
});
有人能解释一下吗?
【问题讨论】:
标签: node.js promise bluebird sinon restify