williamkapke / mongo-mock

Let's pretend we have a real MongoDB
MIT License
240 stars 74 forks source link

How to mock reject behavior? #57

Open assapir opened 6 years ago

angelo-hub commented 6 years ago

Can you add more description around this? Maybe an example of an expected input and output, or else I feel like this should be closed.

mt-sebastien-robert commented 5 years ago

If this is a way to simulate failure, I agree having this would be awesome. Something to have commands (findOne, updateMany, etc...) to fail and throw, to ensure error handling is properly implemented.

@meijin007 Could you confirm this is what you were referring to ? Thanks.

assapir commented 5 years ago

Yes, this is what I was referring to

mt-sebastien-robert commented 5 years ago

@williamkapke Any update or any thought on that ? Thanks.

williamkapke commented 5 years ago

I wonder if you can accomplished it with https://www.npmjs.com/package/proxyquire?

Otherwise- I'm sure something could be worked out. Can you give example code of how you hope for the feature to work?

mt-sebastien-robert commented 5 years ago

A very simple approach could be a way to set a flag that would make all subsequent requests to throw, like in case of network error, or DB process down (which is probably the same).

Different flags could be used to throw different kind of errors, again, network, data corruption or else, sorry I'm familiar with all MongoDB errors, the most I know are network errors.

The goal is to test what happens when my code can properly connect and start to do things, but then at a random point in time, no more network. Does all my code is correctly resilient to failure, this is the best way to test it.

mt-sebastien-robert commented 5 years ago

Thank you for pointing proxyquire by the way, but I don't think it's going to help in this scenario. This failure simulation feature needs to come from within I believe.