코드 반복을 제거하기 위해 Node.js에 대한 자신의 얇은 mongodb 래퍼를 작성했습니다.자신의 mongodb 래퍼를 테스트하는 방법
그러나 Mocha 및 Should와 함께 실행되는 비동기 단위 테스트에 문제가 있습니다.
Should 라이브러리에 의해 Throw 된 예외가 모카가 아닌 MongoDB 드라이버에 의해 캐치되는 경우가 발생합니다. 즉, Mocha는 오류를 catch하지 않으며 done() 함수도 호출되지 않습니다. 결과적으로 모카는 오류 Error: timeout of 2000ms exceeded
을 인쇄합니다. 니핏 랩퍼 모듈
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);
});
};
모카 test.js
같은 문제는 문제를 해결할 수있는 방법이 있는가 간단한 test.js
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();
});
});
});
하여 시뮬레이션 할 수
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();
});
});
});
? 이러한 코드를 테스트 할 수있는 방법이 있어야합니다.
아이디어가있는 사람이 실제로 없습니까? 나는 여러 번 문제를 분석하려고했지만 어떤 해결책도 찾아 내지 못했다. 이것은 내 DB 코드를 전혀 테스트 할 수 없다는 것을 의미합니다. –