How to mock a model using sinon.js?

96 views
Skip to first unread message

Slim

unread,
Sep 27, 2017, 6:32:11 AM9/27/17
to Sinon.JS
Hi,
I have a user model:
const Model = require('objection').Model;

class User extends Model {
 static get tableName() {
   return 'user';
 }

  static get jsonSchema() {
   return {
     type: 'object',
     required: ['name', 'address'],

      properties: {
       id: { type: 'integer' },
       name: { type: 'string', minLength: 1, maxLength: 255 },
       address: { type: 'string', minLength: 1, maxLength: 255 },
     },
   };
 }
}

module.exports = User;

And I want to make some unit test such as verify the insertion of a new user of read a user from a database.
User.test.js:
const sinon = require('sinon');
const user = require('../../../server/models/user');

describe('Test the model user', () => {
const userMock = sinon.mock(user);
test('It should returns a user ', () => {
const userExpected = {
name: 'Provider 1 ',
address: 'Address 1'
};
userMock.expects('query').once().withArgs().returns(userExpected);
user.query();
userMock.restore();
userMock.verify();
});
});

So the test passed well. How can the test fail?







Reply all
Reply to author
Forward
0 new messages