在进行任何其他描述之前,我会在 after() 调用中进行清理。如果所有测试都通过了,清理工作就完成了。但是如果任何测试失败,清理代码将收到一个错误:[错误:没有打开的连接]。
我认为 mongodb 回调中的断言会引发错误,导致连接关闭。这让我很困惑:
- 首先,我认为 mongodb 的回调是放置一些断言的正确位置;
- 其次,断言失败时会抛出错误,并导致连接关闭;
- 最后,由于连接关闭,清理将失败。
那么,即使断言失败,我还应该做些什么来进行清理以完成其工作?
我在下面做了一个示例代码:
var mongo = require('mongoskin')
, should = require('should')
;
describe('mongo', function() {
var db;
before(function() {
console.log('before');
db = mongo.db('devstack.local:27017/test')
});
after(function(done) {
console.log('after');
db.dropDatabase(function(err) {
should.not.exist(err);// [Error: no open connections]
db.close(done);
});
});
describe('close', function() {
it('should count!=0', function(done) {
db.collection('empty').count(function(err, count) {
count.should.not.equal(0); // use an empty collection to make sure this fail
done();
});
})
})
})