How can the following function, intended to add routes to express.js app based on an object hierarchy, be tested using vows.js cleanly without breaking vows' separation of the topic and the vow?
var addRoutes = function(routeObject, app, path) {
var httpVerbs = ['get', 'post', 'put', 'delete'];
path = path || '';
for(var property in routeObject){
var routesAdded = false;
for (var verbIndex in httpVerbs) {
var verb = httpVerbs[verbIndex];
var completePath, handler;
if (property === verb) {
if (typeof(routeObject[verb]) === 'function') {
handler = routeObject[verb];
completePath = path;
} else {
handler = routeObject[verb].handler;
completePath = path + (routeObject[verb].params || '');
}
app[verb](completePath, handler);
routesAdded = true;
}
}
if (!routesAdded) {
addRoutes(routeObject[property], app, path + '/' + property);
}
}
};
The test should confirm that functions corresponding to http verbs are called on the app object. e.g. if routeObject is:
{
tracker: {
message: {
get: {
handler: function (req, res) {
res.send("respond with a resource");
}
}
}
}
}
then
app.get('/tracker/message', function (req, res) {
res.send("respond with a resource");
});
should be called.
Something like this should work: