【问题标题】:mocha mongoose error摩卡猫鼬错误
【发布时间】:2012-12-03 10:39:53
【问题描述】:

当 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);
        });
    });

});

【问题讨论】:

    标签: node.js mongodb mongoose mocha.js


    【解决方案1】:

    完成后关闭连接:

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

    【讨论】:

    • 我认为这通常可以工作,但我有更多的测试,所以不知道在哪里放置 .after()。
    • 将所有测试包装在一个没有测试的describe() 中,只有一个after() 块(以及其余的describe() 块),您可以在其中放置close() 调用。
    猜你喜欢
    • 2014-11-22
    • 2015-07-18
    • 2016-12-26
    • 2015-03-21
    • 2013-05-24
    • 2012-10-30
    • 2015-05-01
    • 2017-06-15
    • 2012-07-30
    相关资源
    最近更新 更多