我正在尝试使用Mockgoose进行测试。每当我在模型上调用.save
或.create
时,承诺都无法解决。我可以毫无问题地使用.find
和.findOne
。我不确定我是否设置了错误的东西,或者我是否误解了应该如何使用Mockgoose。
我的测试设置为:
测试/ 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 ');
});
});
});