【问题标题】:How can this function be tested with vows?如何使用 vows 测试此功能?
【发布时间】:2013-08-08 00:39:51
【问题描述】:

如何使用 vows.js 干净地测试以下函数,该函数旨在基于对象层次结构向 express.js 应用程序添加路由,而不会破坏 vows 的主题和 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);
        }
    }
};

测试应该确认在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");
                        });

应该被调用。

【问题讨论】:

    标签: node.js unit-testing express vows


    【解决方案1】:

    这样的事情应该可以工作:

    var assert = require('assert'),
        request = require('request');
    
    var verbs = ['get', 'post', 'put', 'delete'],
        app = require('your/app.js'),
        port = 8080;
    
    //
    // Your route would look something like this
    //
    var example = {
      tracker: {
        message: {
          get: {
            status: 200,
            body: 'respond with a resource'
          }
        }
      }
    };
    
    function testRoutes(suite, route, app, path) {
      path = path || '/';
      Object.keys(route).forEach(function (key) {
        if (~verbs.indexOf(key)) {
          return testRoutes(suite, route[key], app, [path, key].join('/'));
        }
    
        var test = [key, path].join(' ');
        suite[test] = {
          topic: function () {
            request({
              method: key,
              uri: 'http://localhost' + port + path
            }, this.callback);
          },
          'should respond correctly': function (err, res, body) {
            assert.isNull(err);
            assert.equal(res.statusCode, route[key].status);
            assert.equal(body, route[key].body);
          }
        }
      });
    
      return suite;
    }
    
    var suite = vows.describe('api');
    testRoutes(suite, example, app);
    suite.export(module);
    

    【讨论】:

    • 谢谢,有没有不启动express直接测试功能的方法?我想存根应用程序对象并检查,获取,发布等是否被适当地调用。问题是我无法从誓言中访问它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2021-11-06
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多