How to use the @lykmapipo/mongoose-test-helpers.mockModel function in @lykmapipo/mongoose-test-helpers

To help you get started, we’ve selected a few @lykmapipo/mongoose-test-helpers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lykmapipo / mongoose-rest-actions / test / unit / get.spec.js View on Github external
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);
github lykmapipo / mongoose-rest-actions / test / unit / delete.spec.js View on Github external
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;
github lykmapipo / mongoose-rest-actions / test / unit / put.spec.js View on Github external
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();
github lykmapipo / mongoose-rest-actions / test / unit / patch.spec.js View on Github external
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();