使用Mockgoose测试失败会导致MongoError:拓扑被破坏

时间:2015-12-03 21:19:35

标签: node.js unit-testing mongoose mocking mocha

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

如果我的某个测试失败了(即由于.should.be.deep.equal()失败),所有后续测试都会失败并显示消息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

有谁知道造成这种情况的原因以及我可以采取哪些措施来解决这个问题?当一个测试失败导致所有后续测试失败时,很难确定实际失败的测试数量。

1 个答案:

答案 0 :(得分:0)

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

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