Mongoose错误被返回为未定义?

时间:2014-02-01 06:32:44

标签: node.js mongodb express mongoose jasmine

我正在尝试使用jasmine测试我的数据库连接。

MongoClient是一个对象并已定义。但是err返回undefined,自然我的测试失败了。如果没有错误,Mongoose是否只返回undefined?

还有另一种检查连接的方法吗?

describe("MongoDB", function() {
it("is there a server running", function(next) {
    var MongoClient = require('mongoose');
    MongoClient.connect('mongodb://127.0.0.1:27017/panther_dev', function(err) {
        expect(err).toBe(null);
        next()
    });
});
});

谢谢

1 个答案:

答案 0 :(得分:0)

检查连接的另一种方法是使用Connection#readyState。它返回以下四个值之一:

  • 0 =已断开连接

  • 1 =已连接

  • 2 =连接

  • 3 =断开连接

因此,在您的示例中,要测试连接,它看起来像:

it("is there a server running", function(next) {
    var MongoClient = require('mongoose');
    MongoClient.connect('mongodb://127.0.0.1:27017/panther_dev', function(err) {
        expect(MongoClient.readyState).toBe(1);
        next();
    });
});
相关问题