I'm writing an app in node.js using express.
I seperated the route definition from express so I will be able to unit test it without mocking out express.
What I did is:
file: usersRoutes.js
var routes = {
getAll: function(req,res) {
var db = req.db; // req.db is initialized through a dedicated middleware function
var usersCollection = db.get('users');
usersCollection.find({},{limit:10},function(e,results){
res.send(results);
});
}
};
module.exports = routes;
file: users.js
var express = require('express');
var router = express.Router();
var routes = require('./usersRoutes');
/* GET users listing. */
router.get('/', routes.getAll);
module.exports = router;
and finally in app.js:
var users = require('./routes/users/users.js');
app.use('/users', users);
Now, I want to write a unit test that checks that req.send is being called with the right parameter.
I'm having trouble figuring out the correct way because the req.send is invoked asynchronously.
What is the correct way to unit test this function?