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

$useHandler and instance methods #31

Closed joelwatson closed 6 years ago

joelwatson commented 7 years ago

Is there a way to add a handler for methods that get called on instances returned from another handler? For example, perhaps I return an array of model instances for findAll, and then one of those instances is later called with update()...can I add a handler for the update() call?

LoveAndCoding commented 6 years ago

Yes and no. $useHandler is called for Model methods but not at the moment for instance methods. Especially starting with Sequelize 4 style code, this get complicated to split out in a good way for testing. I'm still tackling how to handle that.

That said, the way I do this in my code at the moment is by overriding the update function on the instance I'm returning. I simply assign it to a mock function that does what I want and use that in my tests.

var myMock = User.build();
myMock.update = function () { /* do stuff */ };
return myMock;

Hope that helps. If you have any more questions I'd be happy to help. I'll try and be quicker on the turn around in the future.

joelwatson commented 6 years ago

Ah, cool, that works. Thanks, @ktsashes!