sequelize-mock icon indicating copy to clipboard operation
sequelize-mock copied to clipboard

How to multiple record mock

Open ina6ra opened this issue 6 years ago • 4 comments

I want to make a mock with multiple records. What kind of method is there?

The following code is the behavior image.

var UserMock = DBConnectionMock.define('users', {
  'email': '[email protected]',
  'username': 'one'
});
var UserMock = DBConnectionMock.define('users', {
  'email': '[email protected]',
  'username': 'two'
});
UserMock.findAll([options]).then(function(users) {
  // users test
});

ina6ra avatar Apr 21 '18 00:04 ina6ra

I was able to solve it self. I used QueryInterface. https://sequelize-mock.readthedocs.io/en/stable/docs/mock-queries/

UserMock = DBConnectionMock.define('users');
UserMock.$queueResult([
  UserMock.build({
    'email': '[email protected]',
    'username': 'one'
  }),
  UserMock.build({
    'email': '[email protected]',
    'username': 'two'
  })
]);
UserMock.findAll([options]).then(function(users) {
  // users test
});

ina6ra avatar Apr 21 '18 16:04 ina6ra

It seems like $queueResult operates for both the findAll and the findOne methods. This seems like a bad design as it means the mocking must know some detail of the implementation of the method you are testing (i.e. are you testing a method that uses findAll or findOne?).

Has there been any discussion of making $queueResult agnostic in this sense?

UserMock.$queueResult(UserMock.build())

UserMock.findAll([options]).then(function(users) {
  // users test
});

UserMock.findOne([options]).then(function(user) {
  // users test
});

jsindos avatar Sep 09 '18 02:09 jsindos

Hope to have better API design to mock multiple mocks, I find it very useful for my tests.

yoonwaiyan avatar Mar 05 '19 07:03 yoonwaiyan

up

marianyfs avatar Jul 09 '19 21:07 marianyfs