I’m using a MySQL DB
and Sequelize
ORM, with mocha
, chai
and sinon
modules for testing. I have a User
model and createUser
method, and I’m trying to use sequelize-mock
to mock the user data.
The code snippet below mocks multiple records in a single mock:
const SequelizeMock = require("sequelize-mock");
const dbMock = new SequelizeMock();
const userMock = dbMock.define("users");
// Mocking multiple records in a single mock
// Reference - github.com/BlinkUX/sequelize-mock/issues/55#issuecomment-383311654
userMock.$queueResult([
userMock.build({
id: 1,
userName: "Superman"
}),
userMock.build({
id: 2,
userName: "Batman"
})
]);
However, userMock.findAll()
only works once and produces varying results in the console log:
createUser test
--------------- 1 -----------------
allUsers = [{"id":1,"userName":"Superman","createdAt":"2020-01-01T09:36:48.286Z","updatedAt":"2020-01-01T09:36:48.286Z"},{"id":2,"userName":"Batman","createdAt":"2020-01-01T09:36:48.286Z","updatedAt":"2020-01-01T09:36:48.286Z"}]
--------------- 2 -----------------
allUsers = [{"id":1,"createdAt":"2020-01-01T09:43:35.030Z","updatedAt":"2020-01-01T09:43:35.030Z"}]
I have two questions:
- Why is the value of
allUsers
varying when called multiple times? - How can I use the same
userMock
multiple times? This is needed for testing the update functionality, as I need to usefindOne()
and update the data of one User record (present inuserMock
), and then find that updated record usingfindOne()
again.
GitHub test repo URL: https://github.com/shrirambalakrishnan/sequelize-mock-test. You can run npm test
to run the tests and look at the console log.