How to use the @lykmapipo/mongoose-test-helpers.mockInstance 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 / put.spec.js View on Github external
it('should work using `put` static method with filter', 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 where = findById.chain('where');
    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');
    const options = { _id: user._id, filter: { name: user.name } };
    User.put(options, 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` instance method', done => {

    const User = createTestModel({}, schema => {
      schema.methods.beforePatch = (updates, done) => done();
      schema.methods.afterPatch = (updates, done) => done();
    }, patch);
    const user = User.fake();

    const mock = mockInstance(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');
    user.patch(updates, (error, updated) => {
      mock.verify();
      mock.restore();

      expect(save).to.have.been.calledOnce;
      expect(beforePatch).to.have.been.calledOnce;
      expect(afterPatch).to.have.been.calledOnce;

      done(error, updated);
    });
github lykmapipo / mongoose-rest-actions / test / unit / post.spec.js View on Github external
it('should work using `post` static method', done => {

    const User = createTestModel({}, schema => {
      schema.methods.beforePost = done => done();
      schema.methods.afterPost = done => done();
    }, post);
    const user = User.fake();

    const mock = mockInstance(user);

    const save = mock.expects('save').yields(null, user);
    const beforePost = sinon.spy(user, 'beforePost');
    const afterPost = sinon.spy(user, 'afterPost');

    User.post(user, (error, created) => {
      mock.verify();
      mock.restore();

      expect(save).to.have.been.calledOnce;
      expect(beforePost).to.have.been.calledOnce;
      expect(afterPost).to.have.been.calledOnce;

      done(error, created);
    });
  });
github lykmapipo / mongoose-rest-actions / test / unit / delete.spec.js View on Github external
it('should work using `del` static method with filter', 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 where = findById.chain('where');
    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');

    const options = { _id: user._id, filter: { name: user.name } };
    User.del(options, (error, deleted) => {
      Mock.verify();
      Mock.restore();

      mock.verify();
      mock.restore();