【发布时间】:2015-09-17 07:02:52
【问题描述】:
我几天来一直试图让它工作。我环顾了互联网和 StackOverflow。有如何使用 MongoDB 测试 API 以及如何编写执行 PSQL 命令的 Mocha 测试的示例。这不是我想要的。
我为pg 创建了一个包装器,名为db.js,来自指令in this SO question(请注意我在调用console.log() 时的cmets:
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);
});
信不信由你, 工作顺利!
我不能在mocha 测试中做同样的事情(即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 || "");
}
);
});
});
帮助!我希望能够使用我的数据库连接编写集成测试。有我缺少的组件吗?需要的库?
这都是手工制作的,我没有使用 IDE,因为我想了解它应该如何自己工作。
提前致谢。
【问题讨论】:
标签: javascript node.js postgresql