摩卡/ Node.js / PostgreSQL集成testing

我一直试图让这个工作好几天。 我浏览过互联网和StackOverflow。 有一些示例说明如何使用MongoDBtestingAPI以及如何编写执行PSQL命令的Mochatesting。 这不是我想要的。

我从这个SO问题的指令中创build了一个叫做db.js的包装器(注意我在对console.log()的调用中的注释:

 pg = require("pg"); config = require("./../config.js"); module.exports = { query: function(text, values, cb) { console.log("I get to this in Mocha"); pg.connect(config.connectionString, function(err, client, done) { console.log("I never get here"); if (err) return console.error("error connecting to postgres: ", err); client.query(text, values, function(err, result) { console.log("I most certainly never get here"); done(); cb(err, result); }) }); } } 

这样做,我可以做到以下几点:

 $ node $ var db = require ("./path/to/db.js"); $ db.query("insert into sometable(id, value) values(1, \"blah\")", {}, function (err, result) { if (err) { console.error ("db errored out man"); } console.log("no error..."); console.log(result); }); 

信不信由你工作顺利!

我不能做的是在mochatesting(即db.spec.js )中同样的事情:

 var db = require("./../../../Data/db.js"); // These tests assume you have run the scripts in the -SQL repo describe("module: db", function() { it("provides a wrapper for the execution of queries", function () { db.query("insert into employer.profile \ (id, returncustomer, receiveupdates, name, email, password, active) \ values (4, true, true, 'someNameLol', 'ce@spam.org', 'change_me', true)", {}, function (err, stdout, stderr) { console.log(err || ""); console.log(stdout || ""); console.log(stderr || ""); } ); }); }); 

帮帮我! 我想能够使用我的数据库连接编写集成testing。 有我缺less的组件吗? 必需的库?

这一切都是手动的,我没有使用IDE,因为我想了解它是如何工作的。

提前致谢。

您需要包含done参数,并在testing结束时调用它。

 describe("module: db", function() { it("provides a wrapper for the execution of queries", function (done) { db.query("insert into employer.profile \ (id, returncustomer, receiveupdates, name, email, password, active) \ values (4, true, true, 'someNameLol', 'ce@spam.org', 'change_me', true)", {}, function (err, stdout, stderr) { console.log(err || ""); console.log(stdout || ""); console.log(stderr || ""); done(); } ); }); });