简体   繁体   中英

Sequelize Mock doesn't work properly with Sequelize function "count"

When I use Sequelize Mock between with a query like that:

 const { active, 'in-progress': inProgress, } = await ImmediateCare.findAll({ raw: true, attributes: ['status', [fn('count', col('id')), 'count']], group: ['ImmediateCare.status'], }).then((status) => { const counts = { active: 0, 'in-progress': 0, } status.forEach((s) => { counts[s.status] = parseInt(s.count, 10) }) return counts })

Is being returned NaN , I'm guessing that is because Sequelize Mock can't mock sequelize.fn('count')

Is that right?

You can use Sinon along with Sequelize Mock and stub the findAll call with mock data. Something like this:

const sinon = require('sinon');
const SequelizeMock = require('sequelize-mock');
describe('', () => {
beforeEach(() => {
const dbConnection = new SequelizeMock();
const dbStub = sinon.stub(dbConnection, 'define');
const queryStub = sinon.stub().resolves(mockImmediateCareData);
dbStub.returns({ findAll : queryStub })
})
})

If this does not help, can you please share the defined mock object? Also, the . then() is not needed as await is already being used.

I was taking the wrong path

This is the test for the method.

I'm not supposed to test the Sequelize count response, I just need to mock the result

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM