-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to test middleware #22
Comments
Do you mean that mocking the |
@gaguirre yes as far as I can tell. That being said I could be mocking the save wrong. ;) Is this something that should work if I am mocking save correctly? |
Probably you are doing it well. The thing is: if you're mocking the @jniemin did you face this issue? |
@gaguirre I think solving in sinon-mongoose or a recipe are both valid options IMHO. I use the mongoose middleware a lot and think its a great way to separate concerns. That being said I have found it very hard to test that middleware. I have tried to just mock the mongoose collection object directly, which in turn really just mocks the mongodb driver. It works in a sense that all mongoose middleware is run when I do this. However I am at the mercy of figuring out what type of object the mongodb driver returns back to mongoose so things still 'work'. That being said, I am also looking at seeing if I can access the middleware 'hooks' in my tests directly. That way I can test the middleware function directly rather than indirectly through a save/update/remove call. |
I didn't run in to it. But if you think about it, you should not need to test if middleware is called in your test. That is basically testing the mongoose logic, not your code. What I would do instead is test the function passed to middleware
unit test
|
As @jniemin said, I think you can unit test the @newmanw tell me if that's clear enough And of course, if you find a way to do it right, please open a PR with a recipe for that ;) |
@gaguirre yes, thanks. Only thing that I have trouble with is testing a non exported function. IE in @jniemin example for that to work I believe you would need to do something like this: In model.js file
Then in test file
Not really optimal. I could possibly get around this using rewire. But no good way IMHO to ask a mongoose model for its 'installed' hooks. |
@newmanw I think it has to be available somewhere in the schema. If you can find where it is then you can test it, and avoid exporting the method in your model. |
What I sometime do is assign them to object that is clearly marked private. There is not really optimal way of exposing "private" function out of modules. So exposing private object at least makes it explicit that it is a private object. Not perfect anyway.
Then you can call it on tests like
|
@newmanw I stumbled upon the same issue recently and "solved" it by exposing the schema middleware function and using lodash.bind() during testing in order to rewrite the I made a tiny example repo with source/tests using this approach. |
Examples are stubbing out the entire mongoose model and seem to be preventing middleware from being called. Is it possible to test mongoose middleware while mocking?
The text was updated successfully, but these errors were encountered: