Rethinkdb与nodejs和expresso
我正在尝试使用rethinkdb
并通过expresso
testing。 我有function
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; });
我正在像这样testing
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
但是,即使在错误的情况下,expresso也显示error 100% 1 tests
。 我试图把throw err;
在catch
,但没有任何变化。
但是,如果我把assert.eql(1, 2, "error");
在setup()
开始时,它按预期失败;
有什么,caching错误? 我该如何让它失败呢? 我find了
Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) { throw e; });
有没有这样的重新思考数据库?
问题是这个testing是asynchronous的,你把它当作一个同步testing。 您需要执行以下操作:
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
函数,它具有更好的asynchronous操作API。 相同的testing看起来像这样:
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 }); });