sequelize-mock
sequelize-mock copied to clipboard
How to multiple record mock
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
});
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
});
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
});
Hope to have better API design to mock multiple mocks, I find it very useful for my tests.
up