4

当 mongoose 尝试连接到 mongodb 时,我在 mocha 测试中收到以下错误:

Error: Trying to open unclosed connection.

这是我的测试:

var cfg = require('../config')
, mongoose = require('mongoose')
, db = mongoose.connect(cfg.mongo.uri, cfg.mongo.db)
, User = require('../models/user')
, Item = require('../models/item')
, should = require('should')
, fakeUser
, fakeItem;

mongoose.connection.on('error', function(err){
    console.log(err);
});

describe('User', function(){
    beforeEach(function(done){
        //clear out db
        User.remove(done);

    });

    after(function(done){
        //clear out db
        User.remove(function(err){
            Item.remove(done);
        });
    });

});
4

1 回答 1

7

完成后关闭连接:

after(function(done){
    //clear out db
    User.remove(function(err){
        Item.remove(function() {
            mongoose.connection.close();
            done();
        });        
    });
});
于 2012-12-16T03:40:07.503 回答