Javascript 如何测试自己的mongodb包装器

Javascript 如何测试自己的mongodb包装器,javascript,node.js,unit-testing,mongodb,mocha.js,Javascript,Node.js,Unit Testing,Mongodb,Mocha.js,我已经为Node.js编写了自己的瘦mongodb包装,以消除代码重复 但是,我在使用Mocha运行异步单元测试时遇到了问题,应该这样做 发生的情况是,应该库抛出的任何异常都被MongoDB驱动程序而不是Mocha捕获。 也就是说,Mocha既没有捕捉到错误,也没有调用done()函数。因此,摩卡打印出一个错误错误:超时超过2000ms 包装器模块的片段db.js var mongodb = require('mongodb').MongoClient; exports.getCollecti

我已经为Node.js编写了自己的瘦mongodb包装,以消除代码重复

但是,我在使用Mocha运行异步单元测试时遇到了问题,应该这样做

发生的情况是,应该库抛出的任何异常都被MongoDB驱动程序而不是Mocha捕获。 也就是说,Mocha既没有捕捉到错误,也没有调用done()函数。因此,摩卡打印出一个错误
错误:超时超过2000ms

包装器模块的片段
db.js

var mongodb = require('mongodb').MongoClient;

exports.getCollection = function(name, callback) {
    mongodb.connect(dbConfig.dbURI, {auto_reconnect: true}, function(err, db) {
        if (err)
            return callback(err, null);

        db.collection(name, {strict: true}, callback);
    });
};
var should = require('should');
var db     = require('./db.js');

describe('Collections', function() {
    it.only('should retrieve user collection', function(done) {
        db.getCollection('user', function(err, coll) {
            should.not.exist(err);
            coll.should.be.a('object');
            // HERE goes an assertion ERROR
            coll.collectionName.should.equal('user123');

            done();
        });
    });
});
var should = require('should');

var obj = {
    call: function(callback) {
        try {
            console.log('Running callback(null);');
            return callback(null);
        }
        catch(e) {
            console.log('Catched an error:', e);
        }
    }
};

describe('Test', function() {
    it('should catch an error', function(done) {
        obj.call(function(err) {
            should.exist(err);
            done();
        });
    });
});
摩卡
test.js

var mongodb = require('mongodb').MongoClient;

exports.getCollection = function(name, callback) {
    mongodb.connect(dbConfig.dbURI, {auto_reconnect: true}, function(err, db) {
        if (err)
            return callback(err, null);

        db.collection(name, {strict: true}, callback);
    });
};
var should = require('should');
var db     = require('./db.js');

describe('Collections', function() {
    it.only('should retrieve user collection', function(done) {
        db.getCollection('user', function(err, coll) {
            should.not.exist(err);
            coll.should.be.a('object');
            // HERE goes an assertion ERROR
            coll.collectionName.should.equal('user123');

            done();
        });
    });
});
var should = require('should');

var obj = {
    call: function(callback) {
        try {
            console.log('Running callback(null);');
            return callback(null);
        }
        catch(e) {
            console.log('Catched an error:', e);
        }
    }
};

describe('Test', function() {
    it('should catch an error', function(done) {
        obj.call(function(err) {
            should.exist(err);
            done();
        });
    });
});
这个简单的
test.js

var mongodb = require('mongodb').MongoClient;

exports.getCollection = function(name, callback) {
    mongodb.connect(dbConfig.dbURI, {auto_reconnect: true}, function(err, db) {
        if (err)
            return callback(err, null);

        db.collection(name, {strict: true}, callback);
    });
};
var should = require('should');
var db     = require('./db.js');

describe('Collections', function() {
    it.only('should retrieve user collection', function(done) {
        db.getCollection('user', function(err, coll) {
            should.not.exist(err);
            coll.should.be.a('object');
            // HERE goes an assertion ERROR
            coll.collectionName.should.equal('user123');

            done();
        });
    });
});
var should = require('should');

var obj = {
    call: function(callback) {
        try {
            console.log('Running callback(null);');
            return callback(null);
        }
        catch(e) {
            console.log('Catched an error:', e);
        }
    }
};

describe('Test', function() {
    it('should catch an error', function(done) {
        obj.call(function(err) {
            should.exist(err);
            done();
        });
    });
});

有没有办法解决这个问题?必须有一种方法来测试这样的代码。

我偶然发现处理了一个不同的问题,但代码让我意识到我可以使用一个简单的技巧让Mocha捕捉断言异常:

describe('Test', function() {
    it('should catch an error', function(done) {
        obj.call(function(err) {
            try {
                should.exist(err);
                done();
            } catch (err) {
                done(err);
            }
        });
    });
});
也就是说,将
should
调用包装到
try/catch
块中,并在catch部分调用
done(err)
执行预期的操作:

  • 如果没有发生断言错误,测试将成功通过
  • 由于
    done()
    函数接受错误参数,如果出现断言错误,测试将失败

  • 由于偶然的运气,我发现正在处理一个不同的问题,但是代码让我意识到我可以使用一个简单的技巧让Mocha捕捉断言异常:

    describe('Test', function() {
        it('should catch an error', function(done) {
            obj.call(function(err) {
                try {
                    should.exist(err);
                    done();
                } catch (err) {
                    done(err);
                }
            });
        });
    });
    
    也就是说,将
    should
    调用包装到
    try/catch
    块中,并在catch部分调用
    done(err)
    执行预期的操作:

  • 如果没有发生断言错误,测试将成功通过
  • 由于
    done()
    函数接受错误参数,如果出现断言错误,测试将失败

  • 真的没有人有主意吗?我多次试图分析这个问题,但没有找到任何解决办法。这意味着我根本无法测试我的DB代码:(难道真的没有人有想法吗?我曾多次尝试分析问题,但没有找到任何解决方案。这意味着我根本无法测试我的DB代码:(