我已经采取了如何使用节点单元和猫鼬获得异步结果?并且非常轻微地修改它以更简单地显示我的失败。
var 猫鼬 = 要求('猫鼬');
var db;
module.exports = {
setUp: function(callback) {
try {
//db.connection.on('open', function() {
mongoose.connection.on('open', function() {
console.log('Opened connection');
callback();
});
db = mongoose.connect('mongodb://localhost/test_1');
console.log('Started connection, waiting for it to open');
} catch (err) {
console.log('Setting up failed:', err.message);
test.done();
callback(err);
}
},
tearDown: function(callback) {
console.log('In tearDown');
try {
console.log('Closing connection');
db.disconnect();
callback();
} catch (err) {
console.log('Tearing down failed:', err.message);
test.done();
callback(err);
}
},
test1: function(test) {
test.ifError(null);
test.done();
},
test2: function(test) {
test.ifError(null);
test.done();
}
};
使用 nodeunit 运行它时,我得到以下信息:
stam2_test.js
Started connection, waiting for it to open
Opened connection
In tearDown
Closing connection
✔ test1
Started connection, waiting for it to open
Opened connection
FAILURES: Undone tests (or their setups/teardowns):
- test2
To fix this, make sure all tests call test.done()
更多信息:如果在 setUp/tearDown 我不使用 mongo 而只是一个测试代码,比如增加一个计数器,这一切都有效。如果我只有一个测试,一切正常。添加另一个测试并在设置中使用 mongo 始终失败,所以我想我在设置中做错了什么。
先感谢您。