简体   繁体   English

Mocha/Node.js/PostgreSQL 集成测试

[英]Mocha/Node.js/PostgreSQL integration testing

I have been trying to get this to work for days.几天来我一直试图让它发挥作用。 I've looked around the internets and on StackOverflow.我环顾了互联网和 StackOverflow。 There are examples of how to test APIs using MongoDB and how to write Mocha tests that execute PSQL commands.有一些示例说明如何使用 MongoDB 测试 API 以及如何编写执行 PSQL 命令的 Mocha 测试。 That's not what I want.那不是我想要的。

I created a wrapper for pg , called db.js from the instructions in this SO question (note my comments in the calls to console.log() :我为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);
            })
        });
    }
}

With that, I can do the following:有了这个,我可以做到以下几点:

$ 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);
        });

Believe it or not, that works without a hitch!信不信由你,可以顺利运行!

What I can't do is the same thing in a mocha test (ie, db.spec.js ):我不能在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 || ""); 
            }
        );
    });
}); 

Help!帮助! I want to be able to write integration tests using my database connection.我希望能够使用我的数据库连接编写集成测试。 Are there components I'm missing?是否有我缺少的组件? Required libraries?需要的库?

This is all hand-rolled, I'm not using an IDE, because I want to understand how it's supposed to work by myself.这都是手工制作的,我没有使用 IDE,因为我想了解它应该如何自己工作。

Thanks in advance.提前致谢。

You need to include the done parameter, and call it at the end of your test.您需要包含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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM