[英]Mocha/Node.js/PostgreSQL integration testing
幾天來我一直試圖讓它發揮作用。 我環顧了互聯網和 StackOverflow。 有一些示例說明如何使用 MongoDB 測試 API 以及如何編寫執行 PSQL 命令的 Mocha 測試。 那不是我想要的。
我為pg創建了一個包裝器,從這個 SO 問題中的說明中稱為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);
});
信不信由你,它可以順利運行!
我不能在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,因為我想了解它應該如何自己工作。
提前致謝。
您需要包含done
參數,並在測試結束時調用它。
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();
}
);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.