BlinkUX / sequelize-mock

A simple mock interface specifically for testing code relying on Sequelize models
https://sequelize-mock.readthedocs.io
MIT License
139 stars 73 forks source link

How to multiple record mock #55

Open ina6ra opened 6 years ago

ina6ra commented 6 years ago

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': 'one@example.com',
  'username': 'one'
});
var UserMock = DBConnectionMock.define('users', {
  'email': 'two@example.com',
  'username': 'two'
});
UserMock.findAll([options]).then(function(users) {
  // users test
});
ina6ra commented 6 years ago

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': 'one@example.com',
    'username': 'one'
  }),
  UserMock.build({
    'email': 'two@example.com',
    'username': 'two'
  })
]);
UserMock.findAll([options]).then(function(users) {
  // users test
});
jsindos commented 5 years ago

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
});
yoonwaiyan commented 5 years ago

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

marianyfs commented 5 years ago

up