Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should work using `getById` static method', done => {
const User = createTestModel({}, schema => {
schema.statics.beforeGetById = (done) => done();
schema.statics.afterGetById = (instance, done) => done();
}, get);
const user = User.fake();
const Mock = mockModel(User);
const findById = Mock.expects('findById');
const exec = findById.chain('exec').yields(null, user);
const beforeGetById = sinon.spy(User, 'beforeGetById');
const afterGetById = sinon.spy(User, 'afterGetById');
User.getById(user._id, (error, found) => {
Mock.verify();
Mock.restore();
expect(findById).to.have.been.calledOnce;
expect(findById).to.have.been.calledWith(user._id);
expect(exec).to.have.been.calledOnce;
expect(beforeGetById).to.have.been.calledOnce;
expect(afterGetById).to.have.been.calledOnce;
done(error, found);
it('should work using `del` static method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforeDelete = done => done();
schema.methods.afterDelete = done => done();
}, del);
const user = User.fake();
const Mock = mockModel(User);
const mock = mockInstance(user);
const findById = Mock.expects('findById');
const exec = findById.chain('exec').yields(null, user);
const remove = mock.expects('remove').yields(null, user);
const beforeDelete = sinon.spy(user, 'beforeDelete');
const afterDelete = sinon.spy(user, 'afterDelete');
User.del(user._id, (error, deleted) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();
expect(findById).to.have.been.calledOnce;
it('should work using `put` static method', done => {
const User = createTestModel({}, schema => {
schema.methods.beforePut = (updates, done) => done();
schema.methods.afterPut = (updates, done) => done();
}, put);
const user = User.fake();
const Mock = mockModel(User);
const mock = mockInstance(user);
const findById = Mock.expects('findById');
const exec = findById.chain('exec').yields(null, user);
const save = mock.expects('save').yields(null, user);
const beforePut = sinon.spy(user, 'beforePut');
const afterPut = sinon.spy(user, 'afterPut');
const updates = _.pick(User.fake(), 'name');
User.put(user._id, updates, (error, updated) => {
Mock.verify();
Mock.restore();
mock.verify();
mock.restore();
it('should work using `patch` static method with filter', done => {
const User = createTestModel({}, schema => {
schema.methods.beforePatch = (updates, done) => done();
schema.methods.afterPatch = (updates, done) => done();
}, patch);
const user = User.fake();
const Mock = mockModel(User);
const mock = mockInstance(user);
const findById = Mock.expects('findById');
const where = findById.chain('where');
const exec = findById.chain('exec').yields(null, user);
const save = mock.expects('save').yields(null, user);
const beforePatch = sinon.spy(user, 'beforePatch');
const afterPatch = sinon.spy(user, 'afterPatch');
const updates = _.pick(User.fake(), 'name');
const options = { _id: user._id, filter: { name: user.name } };
User.patch(options, updates, (error, updated) => {
Mock.verify();
Mock.restore();
mock.verify();