Skip to content

Instantly share code, notes, and snippets.

@fastfrwrd
Created November 7, 2015 20:04
Show Gist options
  • Save fastfrwrd/fbfb996ad84c0278eacd to your computer and use it in GitHub Desktop.
Save fastfrwrd/fbfb996ad84c0278eacd to your computer and use it in GitHub Desktop.
const MyModel = require('./mymodel.js');
describe('My Model', () => {
let model;
beforeEach(() => {
model = new MyModel();
});
describe('hooks', () => {
describe('pre save', () => {
beforeEach(() => {
sinon.spy(modelUtils, 'myMethod');
});
afterEach(() => {
modelUtils.myMethod.restore();
});
it('should use modelUtils.myMethod', (done) => {
model.save((err) => {
try {
expect(modelUtils.myMethod).to.have.been.called;
} catch (assertErr) {
err = err || assertErr;
}
done(err);
});
});
});
});
});
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const utils = require('./utils');
const myModelSchema = new Schema({
hello: 'world'
});
myModelSchema.pre('save', (next) => {
utils.myMethod.call(this);
next();
});
module.exports = mongoose.model('MyModel', myModelSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment