I am new to Mocha + Chai. I wrote a few simple tests that calls my app routes and validates the responses and it works great for endpoints that do not have dependencies to other APIs or to a database.
Now I am struggling trying to find a way to test a route I have that internally ends up calling a database. I can’t actually call the database so I am look for a way to mock it. I am reading on sinon but I fail to understand if what I need is possible with it, and how.
My tests are like:
it('should return 200 when data is valid', function (done) {
chai
.request(app)
.post('/my/endpoint')
.accept('application/json')
.type('json')
.send({
data: '1234'
})
.end(function (err, res) {
res.should.have.status(200);
//other assertions
done();
});
});
The “/my/endpoint” calls a service that then calls a mongo database.
Is there a simple way to mock the db call?
I already extracted the database client instance to a separate file in hopes I could easily mock it.