testing与Mockgoose失败导致MongoError:拓扑被破坏

我正在用Mockgoose / Mongoose(使用Mocha / Chai作为testing套件)进行一系列的testing。

如果我的某个testing发生故障(例如,由于.should.be.deep.equal()失败),所有后续testing都会失败,并显示消息MongoError: topology was destroyed

这里有一些相关的片段:

 mockgoose(mongoose); before(function(done) { mongoose.connect('mongodb://fake.test/TestingDB', function(err) { done(err); }); }); afterEach(function(done) { mockgoose.reset(); done(); }); // Test Cases describe('Testing the functions that deal with users and locations:', function() { // Test Setup var req beforeEach(function(done) { req = {}; mockgoose.reset(); done(); }); beforeEach(function(done) { sensors.create(testData.deviceData, function(err, model) { if (err) {console.log(err)}; done(); }); }); //tests start here 

这里是我得到的错误的一个例子:

 1) Testing functions that use the Furnace collections Testing furnaceOn function Should produce some output: Uncaught TypeError: Cannot read property 'should' of undefined at C:\Users\Zachary Jacobi\Development\webapp\tests\unit\dbFunctionMockTests.js:417:11 at Query.<anonymous> (C:\Users\Zachary Jacobi\Development\webapp\lib\dbFunctions.js:499:3) at C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\kareem\index.js:177:19 at C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\kareem\index.js:109:16 2) Testing functions that use the Furnace collections "before each" hook for "Should produce the same results as the mock up from testData": MongoError: topology was destroyed at Server.insert (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:951:49) at Server.insert (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\lib\server.js:324:17) at executeBatch (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\lib\bulk\unordered.js:436:23) at executeBatches (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\lib\bulk\unordered.js:457:5) at UnorderedBulkOperation.execute (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\lib\bulk\unordered.js:515:44) at bulkWrite (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\lib\collection.js:582:8) at Collection.insertMany (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\lib\collection.js:477:44) at Collection.insert (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\mongodb\lib\collection.js:753:15) at NativeCollection.(anonymous function) [as insert] (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:136:28) at model.Model.$__handleSave (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\model.js:130:21) at model.Model.$__save (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\model.js:189:9) at model.Model.save (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\model.js:282:17) at model._done (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\hooks-fixed\hooks.js:101:24) at _next (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\hooks-fixed\hooks.js:64:28) at fnWrapper (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\hooks-fixed\hooks.js:186:18) at model.Object.defineProperty.value.fn (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\schema.js:250:9) at _next (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\hooks-fixed\hooks.js:62:30) at fnWrapper (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\node_modules\hooks-fixed\hooks.js:186:18) at C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\schema.js:233:13 at complete (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\document.js:1131:5) at C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\document.js:1157:20 at Mixed.SchemaType.doValidate (C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\schematype.js:654:22) at C:\Users\Zachary Jacobi\Development\webapp\node_modules\mongoose\lib\document.js:1153:9 

有谁知道是什么原因造成的,我能做些什么来解决这个问题? 当一次失败的testing导致所有后来的testing失败时,很难确定有多lesstesting实际上失败了。

更新到Mockgoose 5.3.0和Mongoose 4.2.9解决了这个问题。

我最好的猜测原因是beforeEach mockgoose.reset()在testing失败的同时运行,导致重置失败,模拟数据库陷入不良状态。