Mocha测试MongoDB:多次测试中未捕获的TypeError

时间:2015-02-14 16:23:08

标签: javascript node.js mongodb mocha

我已经编写了一个测试,但它不断产生Uncaught TypeError。 我可以单独运行这两个测试。但是当我一起进行两次测试时,就会出现问题。 为了重用连接,我写了一个文件来连接到Mongodb

database.js

var MongoClient = require('mongodb').MongoClient;
var appDb = null;
var url = 'mongodb://localhost:27017/theDB'

exports.connectToServer = function(callback) {
     MongoClient.connect(url, function(err, db) {
            if(err) console.log(err);
            appDb = db;
            callback();
      });
 };

 exports.getDb = function() {
      if(typeof appDb !== 'undefined') {
             return appDb;
       }
 };

然后我在同一目录/ test中进行了两次测试 但是当我通过绑定mocha来测试Mocha时,test1运行良好,但test2不会运行。 它说:

1) Project test suite 'before all' hook:
   Uncaught TypeError: undefined is not a function

test1.js

var appDb = require('./database');
var Q = require('q');
var EnquiryForm = require('../EnquiryForm');
var EnquiryFormReset = require('../EnquiryFormReset');
var should = require('should');

describe('EnquiryForm', function() {
      before(function(done) {
            appDb.connectToServer(done);
      });

      describe('.getValue(key)', function() {
            it('should get Key', function() {
               // test using promise;
            })
       });

       after(function() {
             var reset = new EnquiryFormReset();
             reset.reset();
       });
   });

test2.js

var appDb = require('../database');
var Q = require('q');
var Project = require('../routes/models/Project');
var Clear = require('./MongoScripts/Clear');
var should = require('should');

describe('Project test suite', function() {

    before('project before', function(done) {
        appDb.connectToServer(done);
    });

    describe('.createProject(project), .getProjectById(id)', function() {
        it('can use the createProject to create and getProjectById to find by id.', function() {
        //   another test
        })
    });

    after('project after', function() {
        var clear = new Clear();
        clear.clear();
    });

});

1 个答案:

答案 0 :(得分:0)

我不知道你是否需要像我一样正确地编写测试,但我写的是这样的:

test1.js

var appDb = require('./database');
var Q = require('q');
var EnquiryForm = require('../EnquiryForm');
var EnquiryFormReset = require('../EnquiryFormReset');
var should = require('should');

require("mocha-as-promised")(); //important

suite('EnquiryForm')

before(function(done) {
    appDb.connectToServer(done);
});

test('should get Key', function() {
   // test using promise;
})

after(function() {
     var reset = new EnquiryFormReset();
     reset.reset();
});

test2.js

var appDb = require('../database');
var Q = require('q');
var Project = require('../routes/models/Project');
var Clear = require('./MongoScripts/Clear');
var should = require('should');

suite('Project test suite');

before('project before', function(done) {
    appDb.connectToServer(done);
});

test('can use the createProject to create and getProjectById to find by id.', function(done) {
//   another test
})

after('project after', function() {
    var clear = new Clear();
    clear.clear();
});

你可以尝试,我希望这有助于你