rochejul / sequelize-mocking

Sequelize extension to deal with data-mocking for testing
MIT License
63 stars 26 forks source link

Can't use useHandler or return null. #28

Closed aconfee closed 5 years ago

aconfee commented 6 years ago

Hello! I currently have the following, mostly default example, code which produces the error 'TypeError: MockDummyDao.$useHandler is not a function.'

import SequelizeMock from 'sequelize-mock';
const dbMock = new SequelizeMock();

const MockDummyDao = dbMock.define('dummy'); 

MockDummyDao.$useHandler((query) => {
  if(query === 'findById') {
    return null;
  };
});

Ultimately, all I'm trying to do is return a null, just null, not an object. This is what Sequelize actually returns when a record can't be found. This will then consistently test the following code in my service:

const dummyRow: any = await this.dummyDao.findById(2)
  .catch((e) => { console.error(e); });

 if(_.isNull(dummyRow) || _.isUndefined(dummyRow)){
    return null;
  }

return new Dummy(dummyRow.get('message'));

Which behaves as expected live.

Any best way to return null, or insight into why $useHandler doesn't exist would be a huge help! Thanks!

rochejul commented 6 years ago

Hi

What is the content of your "sequelize-mock"? Could you provide a tiny example to illustrate it please?

Moreover, if I read well: we could see this:

Note: This is not an equivalent mock of Sequelize's built in QueryInterface at the moment. The built in object for Sequelize is undocumented and is marked as @private in their code, meaning it is not likely something to be relied on. If this changes it can be mocked here. Functions have been prefixed with the mock prefix ($) for this reason.

That means these methods are considerated as private and should not be called direclty.

Anyway, I could have a look to understand what after the mocking, these methods are not exposed

Many thanks

Regards

rochejul commented 6 years ago

See https://sequelize-mock.readthedocs.io/en/stable/api/queryinterface/

rochejul commented 5 years ago

Close issue, due to no more feedbacks