我使用的是一个数据库库,它的基于回调的接口如下所示:
var DB = {
insert: function(options, callback) {
}
}我想在这个数据库周围实现一个包装器,以将其回调风格的API转换为基于promise的API。为此,我定义了以下类:
var DatabaseWrapper = {
init: function(db) {
this.db = db;
},
insert: function(options) {
return Q.denodeify(this.db.insert.bind(this.db))(options);
}
}我想要编写一个单元测试,以确保当我调用DatabaseWrapper.insert时,它会调用DB.insert。到目前为止,我的测试如下所示:
describe('DatabaseWrapper', function () {
var wrapper, insertSpy, bindStub;
beforeEach(function () {
wrapper = Object.create(DatabaseWrapper);
insertSpy = sinon.spy(function () {
console.log('insertSpy got called');
});
bindStub = sinon.stub();
wrapper.db = {
insert: function (options, callback) {
}
};
sinon.stub(wrapper.db.insert, 'bind').returns(insertSpy);
});
describe('#insert', function () {
it('should delegate to db.insert', function (done) {
wrapper.insert({herp: 'derp'});
expect(wrapper.db.insert.bind).to.have.been.calledOnce;
// This fails but I expect it to succeed
expect(promise).to.have.been.calledOnce;
})
});
});DB实例的insert方法实际上是在测试失败后调用的,因为在控制台中输出了'insertSpy got called'消息。
但显然,它是在测试失败后调用的。
据我所知,这是由于Node的process.nextTick的工作方式。因此,对回调的调用发生在测试失败之后。有没有办法在不依赖第三方库(例如q-flush)的情况下修复这个测试?
发布于 2015-05-30 22:14:11
您正在执行异步操作,因此最好执行异步测试。添加setTimeout仍然会使您容易出现竞争情况。
describe('#insert', function () {
it('should delegate to db.insert', function () { // no done here
// note the return here to signal to mocha this is a promise test
return wrapper.insert({herp: 'derp'}).then(function(){
// add expects here, rest of asserts should happen here
expect(wrapper.db.insert.bind).to.have.been.calledOnce;
});
})
});
});https://stackoverflow.com/questions/30544044
复制相似问题