5

I am trying to use Mockgoose for my tests. Whenever I call .save or .create on a model, the promise doesn't resolve. I am able to use .find and .findOne with no problems. I'm not sure if I have set up something incorrectly or if I misunderstand how Mockgoose is supposed to be used.

My tests are set up as:

test/mockgoose.js

const mongoose = require('mongoose');
const { Mockgoose } = require('mockgoose');

const mockgoose = new Mockgoose(mongoose);

mockgoose.helper.setDbVersion('3.2.1');
mongoose.Promise = global.Promise;

module.exports = {
    mockgoose,
    mongoose
};

model.test.js

const { mongoose, mockgoose } = require('../test/mockgoose');
let Model = require('../model.js');
let conn;

describe('something', function() {
     before(() => {
        return mockgoose.prepareStorage().then(() => {
            mongoose.connect('mongodb://127.0.0.1:27017/testingDB');
            return new Promise(resolve => {
                mongoose.connection.once('connected', () => {
                    conn = mongoose.connection;
                    Model = Model(conn);
                    // we pass the connection into every model because we have several connections
                    resolve();
                });
            });
        });
    });

    after(() => {
        mongoose.disconnect();
    });

    it('should save', function() {

        const modelInstance = new Model({'name': 'model name'});
        /* modelInstance looks correct, 
        I have plugins adding additional fields 
        and those are being added correctly at this point */

        return modelInstance.save().then(newModel => {
            /* console.log is never hit and mocha timeouts
            if I console.log modelInstance.save() 
            I can see it is a pending promise. */

            console.log(newModel);
            assert.equal(newModel.additionalField, 'a', 'field is added '); 
        });
    });
});
LT-
  • 371
  • 1
  • 8
  • Did you manage to resolve this? – Ally Murray Oct 12 '17 at 19:58
  • No, I haven't been able to resolve this yet. – LT- Oct 13 '17 at 04:39
  • I am having the same problem and looking about online it would appear we aren't the only two with this problem. Have you submitted a GitHub issue? If not I will submit one over the weekend. The owner of the project appears to be good at responding to issues. – Ally Murray Oct 13 '17 at 08:17
  • Ah! sorry, it took so long for me to respond. I have not submitted an issue yet, were you able to do that? Have you heard a response? – LT- Oct 20 '17 at 22:23

0 Answers0