如何使用 vows.js 干净地测试以下函数,该函数旨在基于对象层次结构向 express.js 应用程序添加路由,而不会破坏誓言对主题和誓言的分离?
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);
}
}
};
测试应确认在 app 对象上调用了对应于 http 动词的函数。例如,如果 routeObject 是:
{
tracker: {
message: {
get: {
handler: function (req, res) {
res.send("respond with a resource");
}
}
}
}
}
然后
app.get('/tracker/message', function (req, res) {
res.send("respond with a resource");
});
应该调用。