Tests failing with Mockgoose cause MongoError: top

2019-07-18 19:17发布

问题:

I'm running a series of tests with Mockgoose/Mongoose (using Mocha/Chai as the test suite).

If one of my tests happens to fail (ie. due to a failed .should.be.deep.equal()), all of the subsequent tests fail with the message MongoError: topology was destroyed

Here's some relevant snippets:

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

And here's an example of the errors I get:

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

Does anyone know what is causing this and what I can do to fix it? It's making it difficult to determine how many tests are actually failing when one failed test causes all subsequent ones to fail.

回答1:

Updating to Mockgoose 5.3.0 and Mongoose 4.2.9 solves this problem.

My best guess as to the cause was the beforeEach mockgoose.reset() running at the same time as tests were failing, causing the reset to fail and the mocked database to get stuck in a bad state.