[英]Rethinkdb with nodejs and expresso
我正在尝试使用rethinkdb
并通过expresso
测试它。 我有功能
module.exports.setup = function() {
var deferred = Q.defer();
r.connect({host: dbConfig.host, port: dbConfig.port }, function (err, connection) {
if (err) return deferred.reject(err);
else deferred.resolve();
});
return deferred.promise;
});
我正在测试它
module.exports = {
'setup()': function() {
console.log("in setup rethink");
db.setup().then(function(){
console.log(clc.green("Sucsessfully connected to db!"));
}).catch(function(err){
console.log('error');
assert.isNotNull(err, "error");
});
}
};
我正在运行这样的代码
expresso db.test.js
但即使error 100% 1 tests
expresso也会显示error 100% 1 tests
。 我试图throw err;
在catch
,但没有任何变化。
但是,如果我把assert.eql(1, 2, "error");
在setup()
的开头,它按预期失败;
有什么东西可以缓解错误吗? 我怎么能让它失败呢? 对于squalize
我找到了
Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) {
throw e;
});
对于重新思考db有没有这样的东西?
问题是这个测试是异步的,你将它视为同步测试。 您需要执行以下操作:
module.exports = {
'setup()': function(beforeExit, assert) {
var success;
db.setup().then(function(){
success = true;
}).catch(function(err){
success = false;
assert.isNotNull(err, "error");
});
beforeExit(function() {
assert.isNotNull(undefined, 'Ensure it has waited for the callback');
});
}
};
摩卡vs快车
你应该考虑看看mocha.js ,它通过传递done
函数为异步操作提供了更好的API。 相同的测试看起来像这样:
module.exports = {
'setup()': function(done) {
db.setup().then(function(){
assert.ok(true);
}).catch(function(err){
assert.isNotNull(err, "error");
})
.then(function () {
done();
});
}
};
承诺
您编写的第一个函数可以通过以下方式重写,因为默认情况下,RethinkDB驱动程序会在所有操作上返回一个promise。
module.exports.setup = function() {
return r.connect({host: dbConfig.host, port: dbConfig.port });
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.