如何在 Foxx 中创建用于测试的时间路径?
How to create temporal routes for testing in Foxx?
我正在尝试向我的 Foxx 应用程序添加一些测试。目前,我需要像这样为 Mocha 中的每个测试用例重新创建一条新路由:
it('allows POST with JSON encoding', function() {
var app = new Foxx.Controller(applicationContext);
app.post(urlString(), graphqlHTTP({
schema: TestSchema
}));
var res = request(app).post(urlString(), {
body: { query: '{test}' },
json: true
});
expect(res.text).to.equal('{"data":{"test":"Hello World"}}');
});
当我 运行 测试时,这条线 var app = new Foxx.Controller(applicationContext);
有问题:
TypeError: Cannot read property 'push' of undefined
2016-02-14T12:58:58Z [12527] INFO /my-app at new Controller (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/server/modules/org/arangodb/foxx/controller.js:330:19)
2016-02-14T12:58:58Z [12527] INFO /my-app at Context.<anonymous> (/usr/local/var/lib/arangodb-apps/_db/ilearn/my-app/APP/src/__tests__/http-test.js:328:17)
2016-02-14T12:58:58Z [12527] INFO /my-app at Test.Runnable.run (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runnable.js:233:15)
2016-02-14T12:58:58Z [12527] INFO /my-app at Runner.runTest (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:390:10)
2016-02-14T12:58:58Z [12527] INFO /my-app at /usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:473:12
2016-02-14T12:58:58Z [12527] INFO /my-app at next (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:315:14)
2016-02-14T12:58:58Z [12527] INFO /my-app at /usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:325:7
2016-02-14T12:58:58Z [12527] INFO /my-app at next (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:260:23)
2016-02-14T12:58:58Z [12527] INFO /my-app at /usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:292:5
2016-02-14T12:58:58Z [12527] INFO /my-app at Function.global.DEFINE_MODULE.exports.nextTick (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/common/bootstrap/modules/process.js:26:3)
... 这基本上是说 applicationContext.foxxes
是 undefined
.
我也明白用globalapplicationContext
做测试不是很明智。但是我怎样才能创建自己的呢?我还没有找到任何相关文件。
在 ArangoDB 中 2.x 无法动态创建控制器。只能在通过 Foxx 服务清单中的 controllers
属性 加载的文件中创建控制器。尽管它们看起来像常规对象,但正如您所注意到的,它们实际上操纵着神奇的 applicationContext.foxxes
属性,它在加载时定义,然后在模块执行时读取。
这意味着无法在您的测试中动态使用 Foxx.Controller
。但是,您可以使用 applicationContext.mount
访问您的服务的挂载点,并以这种方式构建指向您的真实控制器的 URL。
在 ArangoDB 3.0 中,可以动态创建路由器(相当于 2.x 控制器),尽管没有工具化在测试中动态安装它们或向它们发送虚假请求。
您的示例看起来实际上是在测试 GraphQL 模式而不是控制器本身。在那种情况下,您可以简单地编写一个测试,将查询传递给架构并直接测试其输出。
或者,您可以将作为请求处理程序传递的回调提取到单独的函数中,并使用伪造的请求和响应对象测试其行为。
通过 ArangoDB 的 HTTP 栈的全栈集成测试应该是 Foxx 测试的最后手段。除了明显的性能问题(在请求期间占用至少两个线程加上实际必须达到 request/response 开销本身),如果您的服务在 运行 中,它也可能导致意外行为开发模式(即传入请求可能会导致服务重新加载,而测试仍在 运行)。
附带说明一下,在控制器、脚本和导出文件以外的文件中使用 applicationContext
是完全可以接受的。上下文始终与文件相关,而不是直接由整个服务共享(尽管它主要只是包装服务本身的属性,使其对服务的所有文件来说都是全局的)。所以在大多数情况下,只要你不直接操作,你就可以在测试中使用它。配置或清单属性。
我正在尝试向我的 Foxx 应用程序添加一些测试。目前,我需要像这样为 Mocha 中的每个测试用例重新创建一条新路由:
it('allows POST with JSON encoding', function() {
var app = new Foxx.Controller(applicationContext);
app.post(urlString(), graphqlHTTP({
schema: TestSchema
}));
var res = request(app).post(urlString(), {
body: { query: '{test}' },
json: true
});
expect(res.text).to.equal('{"data":{"test":"Hello World"}}');
});
当我 运行 测试时,这条线 var app = new Foxx.Controller(applicationContext);
有问题:
TypeError: Cannot read property 'push' of undefined
2016-02-14T12:58:58Z [12527] INFO /my-app at new Controller (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/server/modules/org/arangodb/foxx/controller.js:330:19)
2016-02-14T12:58:58Z [12527] INFO /my-app at Context.<anonymous> (/usr/local/var/lib/arangodb-apps/_db/ilearn/my-app/APP/src/__tests__/http-test.js:328:17)
2016-02-14T12:58:58Z [12527] INFO /my-app at Test.Runnable.run (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runnable.js:233:15)
2016-02-14T12:58:58Z [12527] INFO /my-app at Runner.runTest (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:390:10)
2016-02-14T12:58:58Z [12527] INFO /my-app at /usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:473:12
2016-02-14T12:58:58Z [12527] INFO /my-app at next (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:315:14)
2016-02-14T12:58:58Z [12527] INFO /my-app at /usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:325:7
2016-02-14T12:58:58Z [12527] INFO /my-app at next (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:260:23)
2016-02-14T12:58:58Z [12527] INFO /my-app at /usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/node/node_modules/mocha/lib/runner.js:292:5
2016-02-14T12:58:58Z [12527] INFO /my-app at Function.global.DEFINE_MODULE.exports.nextTick (/usr/local/Cellar/arangodb/2.8.0/share/arangodb/js/common/bootstrap/modules/process.js:26:3)
... 这基本上是说 applicationContext.foxxes
是 undefined
.
我也明白用globalapplicationContext
做测试不是很明智。但是我怎样才能创建自己的呢?我还没有找到任何相关文件。
在 ArangoDB 中 2.x 无法动态创建控制器。只能在通过 Foxx 服务清单中的 controllers
属性 加载的文件中创建控制器。尽管它们看起来像常规对象,但正如您所注意到的,它们实际上操纵着神奇的 applicationContext.foxxes
属性,它在加载时定义,然后在模块执行时读取。
这意味着无法在您的测试中动态使用 Foxx.Controller
。但是,您可以使用 applicationContext.mount
访问您的服务的挂载点,并以这种方式构建指向您的真实控制器的 URL。
在 ArangoDB 3.0 中,可以动态创建路由器(相当于 2.x 控制器),尽管没有工具化在测试中动态安装它们或向它们发送虚假请求。
您的示例看起来实际上是在测试 GraphQL 模式而不是控制器本身。在那种情况下,您可以简单地编写一个测试,将查询传递给架构并直接测试其输出。
或者,您可以将作为请求处理程序传递的回调提取到单独的函数中,并使用伪造的请求和响应对象测试其行为。
通过 ArangoDB 的 HTTP 栈的全栈集成测试应该是 Foxx 测试的最后手段。除了明显的性能问题(在请求期间占用至少两个线程加上实际必须达到 request/response 开销本身),如果您的服务在 运行 中,它也可能导致意外行为开发模式(即传入请求可能会导致服务重新加载,而测试仍在 运行)。
附带说明一下,在控制器、脚本和导出文件以外的文件中使用 applicationContext
是完全可以接受的。上下文始终与文件相关,而不是直接由整个服务共享(尽管它主要只是包装服务本身的属性,使其对服务的所有文件来说都是全局的)。所以在大多数情况下,只要你不直接操作,你就可以在测试中使用它。配置或清单属性。