2012-12-20 38 views
3

我正在爲mongodb周圍的操作編寫nodeunit測試。當我使用nodeunit(nodeunit testname.js)執行測試時,測試運行並變綠,但nodeunit命令行不返回(我需要按Ctrl-c)。爲什麼我的munodb的nodeunit測試沒有完成?

我在做什麼錯?我需要關閉數據庫連接或服務器,還是我的測試錯誤?

這是一個截取樣本測試。

process.env.NODE_ENV = 'test'; 
var testCase = require('/usr/local/share/npm/lib/node_modules/nodeunit').testCase; 
exports.groupOne = testCase({ 
    tearDown: function groupOneTearDown(cb) {  
    var mongo = require('mongodb'), DBServer = mongo.Server, Db = mongo.Db; 
    var dbServer = new DBServer('localhost', 27017, {auto_reconnect: true}); 
    var db = new Db('myDB', dbServer, {safe:false}); 

    db.collection('myCollection', function(err, collectionitems) { 
      collectionitems.remove({Id:'test'}); //cleanup any test objects 
     }); 

    cb(); 
}, 
aTest: function(Assert){ 
    Assert.strictEqual(true,true,'all is well'); 
    Assert.done(); 
} 
}); 

邁克爾

+0

我也有這個問題。在我看來,MongoDB連接根本就沒有關閉。你有沒有得到更多的信息沒有這個問題? – juriejan

回答

2

嘗試把你的cb()remove()回調中後您關閉連接:

var db = new Db('myDB', dbServer, {safe:false}); 

db.collection('myCollection', function(err, collectionitems) { 
    collectionitems.remove({Id:'test'}, function(err, num) { 
     db.close(); 
     cb(); 
    }); 
}); 
+0

嗯,試過了和其他幾個變種,沒有運氣。 –

+0

是否有其他代碼在運行?你正在做任何其他連接? – theabraham

0

您需要調用cb功能db關閉(在tearDown ):

tearDown: function(cb) { 

    // ... 
    // connection code 
    // ... 

    db.collection('myCollection', function(err, collectionitems) { 
     // upon cleanup of all test objects 
     db.close(cb); 
    }); 
} 

這適用於我。

相關問題