Sunday, 14 May 2017

How to hit a path in branch coverage where a callback is being used

I started to istanbul as a test coverage tool with mocha and one of the great things is that it shows you the paths (branches) you have tested in your test code logic.

There is a path that can only be taken if a error occurs on the database.

Screenshot of the part I am interested in testing

The [I] indicates the the first if was not tested. The problem is that it uses a callback function(err, data) and the error is passed to this callback through a the mongoose model method find(), and because of that I don't have the flow control of this part of the code.

In this specific case I using supertest which is a module to test routes in node.js and it make requests to a route that calls a mongoose model method find() which doesn't throw an error even if the database doesn't have any data.

What would be the best option to test this path? Create a stub to simulate the method? Or just remove the if?



via Alexandre Alencar

No comments:

Post a Comment