如何测试快速配置?

时间:2015-08-30 18:42:44

标签: node.js unit-testing testing express config

我一直在试图弄清楚如何测试快速服务器的配置(例如中间件等)。我无法找到任何示例,我不确定测试它的最佳方法是简单地将预期的中间件列表与实际的中间件列表匹配,完全做其他事情,或者即使它只是配置根本不应该进行测试。

我还应该补充一点,我对如何做到这一点并不感兴趣,而是更高层次的概念。但是如果它有帮助的话我会使用摩卡和超级。

编辑:抱歉,我应该更清楚了。我正在尝试测试添加&的文件配置所有中间件。不是中间件本身。

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您希望测试的内容:

  • 如果您需要测试现有的中间件插件,大多数都会将测试作为其分发的一部分。在nodes-modules / MODULE-NAME目录中,只需调用npm test

  • 如果您希望测试自己的中间件,则需要自己编写这些测试,可能使用现有中间件中提供的测试作为示例。

  • 如果您希望测试当前在Express中配置哪些中间件插件,除非您在快速实例中明确添加它们,否则没有。

  • 如果您希望通过中间件链测试请求,可以将自己的中间件间谍添加到您需要的请求链中。

最后,如果你想测试表达自己,它的测试目录中有很多测试,就像大多数快速中间件发行版一样,这是节点的卖点之一:它的所有模块都为你提供了源代码探索。

已更新

感谢您的澄清。

快递' app.use()如果你没有参数调用它会抛出异常,或者传递一个未定义的参数或null,你可以非常肯定你传递的任何函数都会被添加到中间件处理级联中。

所以,假设:

var express=require('express'),
    app=express(),
    undefinedVariable;

致电以下任何一项:

app.use();
app.use(undefinedVariable);
app.use(null);

将失败:

.../node-modules/express/lib/application.js:209
throw new TypeError('app.use() requires middleware functions');
      ^
TypeError: app.use() requires middleware functions
    at EventEmitter.use (.../node-modules/express/lib/application.js:209:11)
    at Object.<anonymous> (.../index.js:7:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

现在,如果您确实想要测试是否已将中间件功能添加到请求处理级联中,您需要检查调用该函数的已知副作用,如:

var util = require('util'),
    assert = require('assert'),
    express = require('express');

// define some helper functions

出于解释的目的,我们将使用此自定义中间件来模拟标准的快速中间件。 (此函数只是添加一个新值来表达标准请求对象。这个新值是&#34;副作用&#34;我们将对其进行测试。)

/**
 * Custom middleware that sets req.hasCustomMiddleware to true.
 *
 * @param {object} req - express-style request
 * @param {object} res - express-style response
 * @param {function} next - express middleware callback
 */
var customMiddleware = function(req,res,next){
  req.hasCustomMiddleware = true;
  next();
};

接下来,如果测试函数没有返回true,则此工厂函数将使用提供的msg抛出异常(通过assert)。 (我们将此工厂用作一种速记方式,以明确定义的方式自动创建一种测试中间件。)

/**
 * Creates a new middleware function that asserts test.
 *
 * @param {string} msg - msg to use if/when assertion fails
 * @param {function} test - function that tests for expected values.
 * @returns {Function}
 */
var middlewareAssertionFactory = function(msg, testFunc) {
  assert('string' === typeof msg && msg);
  assert('function' === typeof testFunc);
  return function(req, res, next) {
    assert(testFunc(req, res), util.format('"%s" middleware assertion failed', msg));
    next();
  };
};

现在我们需要一个函数来确保我们感兴趣的副作用值存在且具有预期值。

/**
 * Test Function for expected value(s).
 *
 * @param {object} req - express-style request
 * @param {object} res - express-style response
 * @returns {boolean} - true if expected values exist
 */
var expectedValueTestFunction = function(req, res) {
  return 'object' === typeof req && (req.hasCustomMiddleware ? true : false);
};

然后我们通过将它传递给我们的middlewareAssertionFactory函数来创建中间件测试函数。

var customMiddlewareTester = middlewareAssertionFactory('req.hasCustomMiddleware exists and is true', expectedValueTestFunction);

最后,我们将Express应用程序配置为使用我们的customMiddleware和customMiddlewareTester函数(必须按此顺序添加),添加一个简单的默认路由并启动我们的服务器。

// config express app

var app = express();

// add our custom middleware
app.use(customMiddleware);

// and our test for the expected side-effect
app.use(customMiddlewareTester);

// add a simple route

app.get('/', function(req, res) {
  res.send('hello world!');
});

// fire up a server

var server = app.listen(3000, function() {
  var addr = server.address();
  console.log('app listening at http://%s:%s', addr.address, addr.port);
});

运行此服务器后,对http://localhost:3000/的请求将返回

hello world!

但是,如果我们更改我们的测试函数以返回false来模拟我们的预期值不存在或没有预期值的情况,如:

var expectedValueTestFunction = function(req, res) {
  return false;
};

并提出相同的要求,我们会看到:

AssertionError: "req.hasCustomMiddleware exists and is true" middleware assertion failed
  at .../index.js:42:9
  at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
  at trim_prefix (.../node_modules/express/lib/router/index.js:312:13)
  at .../node_modules/express/lib/router/index.js:280:7
  at Function.process_params (.../node_modules/express/lib/router/index.js:330:12)
  at next (.../node_modules/express/lib/router/index.js:271:10)
  at customMiddleware (.../index.js:16:7)
  at Layer.handle [as handle_request] (.../node_modules/express/lib/router/layer.js:95:5)
  at trim_prefix (.../node_modules/express/lib/router/index.js:312:13)
  at .../node_modules/express/lib/router/index.js:280:7

使用此策略,您应该能够为任何导致更改标准请求或响应对象的快速中间件构建测试,我相信这些更改包括大多数(如果不是全部)。