I don't know how I'd term this maybe 'static call to a koa router'? Does that seem like the right wordage here for what I'm really trying to accomplish if you were to talk about it technically?
我不知道我怎么称这可能是“静态呼叫koa路由器”?如果你在技术上谈论它,这对于我真正想要完成的事情来说,这似乎是正确的语言吗?
Anyway, I'm using koa-router and I'm coding unit tests (not integration tests). So I do not want to invoke .listen() on my koa app because of that reason...it would create an http server which now makes my test an integration tests.
无论如何,我正在使用koa-router而我正在编码单元测试(不是集成测试)。所以我不想在我的koa应用程序上调用.listen()因为这个原因...它会创建一个http服务器,现在我的测试是集成测试。
Instead in my test I simply want to make a straight call to the app object instance and call a route and be able to return no results and check that I returned no results in the response.
相反,在我的测试中,我只是想直接调用app对象实例并调用路由并且能够返回没有结果并检查我在响应中没有返回任何结果。
How can you do that? I can't find an example and I've tried all sorts of pseudo code attemps agains the koa app object.
你怎么能这样做?我找不到一个例子,我已经尝试了各种伪代码尝试koa app对象。
1 个解决方案
#1
4
If you want to test the function that koa-router routes to then just perform a unit test on that function and leave the routing out of it.
如果要测试koa-router路由到的功能,则只需对该功能执行单元测试,并将路由保留在其中。
To me it sounds like you've got a file such as app.js and it contains all your code. What you can do is create a router.js file to put you route bindings and a services.js file where you can put your application logic.
对我而言,听起来你有一个像app.js这样的文件,它包含你所有的代码。您可以做的是创建一个router.js文件来为您提供路由绑定和一个services.js文件,您可以在其中放置您的应用程序逻辑。
So for example app.js might look like:
所以例如app.js可能看起来像:
var koa = require("koa");
var app = module.exports = koa();
var router = require('./router.js');
app.use(router.unsecured.middleware());
app.listen(3000);
And router.js might look like:
而router.js可能看起来像:
var router = require("koa-router");
var service = require("./services.js");
var unsecured = module.exports.unsecured = new router();
unsecured.post('/account/signin', service.signinUser);
unsecured.post('/account/register', service.registerUser);
And services.js might look like:
而services.js可能看起来像:
module.exports.signinUser = function*(signinDetails) {
// contains your application signin logic
};
module.exports.registerUser = function*(registerDetails) {
// contains your application register logic
};
So in this manner you can individually test services.js. I don't see any value in individually testing router.js since it is so trivial. As @Dan Pantry shows you can test routing as part of an integration test using supertest.
因此,您可以通过这种方式单独测试services.js。我没有看到单独测试router.js的任何价值,因为它是如此微不足道。正如@Dan Pantry所示,您可以使用supertest测试路由作为集成测试的一部分。
Edit:
So this is a little experimental test I was playing around with to test that the routing is correct. I'm using mocha as the test runner and the code example I posted in my original code.
所以这是我正在玩的一个小实验测试来测试路由是否正确。我使用mocha作为测试运行器和我在原始代码中发布的代码示例。
// standard library
var assert = require("assert");
// in app objects
var router = require('./router.js');
var service = require('./service.js');
describe("routing tests", function() {
it("test register routing, POST", function*(done) {
// arrange
var unsecured = router.unsecured;
var path = '/account/register';
var httpMethod = 'POST';
var expected = service.register.toString();
var actual;
// act
for (var i = 0; i < unsecured.stack.length; i++)
{
var pathMatch = unsecured.stack[i].path === path;
var methodMatch = unsecured.stack[i].methods.indexOf(httpMethod) >= 0;
if (pathMatch && methodMatch)
{
actual = unsecured.stack[i].middleware.toString();
break;
}
}
// assert
try {
assert.equal(expected, actual);
done();
} catch(err) {
done(err);
}
});
});
There is probably a neater way of doing this (and a more modular way for testing multiple paths) but as I said this is just a basic example to verify the routing is calling the correct service. What I'm doing is delving into the koa-router object to verify what path is bound to what service code depending on the HTTP method (e.g. POST, GET, etc).
可能有一种更简洁的方法(以及一种更模块化的方式来测试多个路径)但正如我所说,这只是验证路由调用正确服务的基本示例。我正在做的是深入研究koa-router对象,以验证哪个路径绑定到哪个服务代码,具体取决于HTTP方法(例如POST,GET等)。
If you have your routing and your services in modules this test completely avoids dealing with the main koa app. Although technically this test spans multiple units (the routing and the service code) so it would technically be an integration test but it does mean you don't go near app.listen()
which is what you didn't want to call in your tests.
如果您在模块中拥有路由和服务,则此测试完全避免处理主要的koa应用程序。虽然从技术上讲这个测试跨越多个单元(路由和服务代码),所以它在技术上是一个集成测试,但它确实意味着你不要靠近app.listen(),这是你不想在你的试验。
#1
4
If you want to test the function that koa-router routes to then just perform a unit test on that function and leave the routing out of it.
如果要测试koa-router路由到的功能,则只需对该功能执行单元测试,并将路由保留在其中。
To me it sounds like you've got a file such as app.js and it contains all your code. What you can do is create a router.js file to put you route bindings and a services.js file where you can put your application logic.
对我而言,听起来你有一个像app.js这样的文件,它包含你所有的代码。您可以做的是创建一个router.js文件来为您提供路由绑定和一个services.js文件,您可以在其中放置您的应用程序逻辑。
So for example app.js might look like:
所以例如app.js可能看起来像:
var koa = require("koa");
var app = module.exports = koa();
var router = require('./router.js');
app.use(router.unsecured.middleware());
app.listen(3000);
And router.js might look like:
而router.js可能看起来像:
var router = require("koa-router");
var service = require("./services.js");
var unsecured = module.exports.unsecured = new router();
unsecured.post('/account/signin', service.signinUser);
unsecured.post('/account/register', service.registerUser);
And services.js might look like:
而services.js可能看起来像:
module.exports.signinUser = function*(signinDetails) {
// contains your application signin logic
};
module.exports.registerUser = function*(registerDetails) {
// contains your application register logic
};
So in this manner you can individually test services.js. I don't see any value in individually testing router.js since it is so trivial. As @Dan Pantry shows you can test routing as part of an integration test using supertest.
因此,您可以通过这种方式单独测试services.js。我没有看到单独测试router.js的任何价值,因为它是如此微不足道。正如@Dan Pantry所示,您可以使用supertest测试路由作为集成测试的一部分。
Edit:
So this is a little experimental test I was playing around with to test that the routing is correct. I'm using mocha as the test runner and the code example I posted in my original code.
所以这是我正在玩的一个小实验测试来测试路由是否正确。我使用mocha作为测试运行器和我在原始代码中发布的代码示例。
// standard library
var assert = require("assert");
// in app objects
var router = require('./router.js');
var service = require('./service.js');
describe("routing tests", function() {
it("test register routing, POST", function*(done) {
// arrange
var unsecured = router.unsecured;
var path = '/account/register';
var httpMethod = 'POST';
var expected = service.register.toString();
var actual;
// act
for (var i = 0; i < unsecured.stack.length; i++)
{
var pathMatch = unsecured.stack[i].path === path;
var methodMatch = unsecured.stack[i].methods.indexOf(httpMethod) >= 0;
if (pathMatch && methodMatch)
{
actual = unsecured.stack[i].middleware.toString();
break;
}
}
// assert
try {
assert.equal(expected, actual);
done();
} catch(err) {
done(err);
}
});
});
There is probably a neater way of doing this (and a more modular way for testing multiple paths) but as I said this is just a basic example to verify the routing is calling the correct service. What I'm doing is delving into the koa-router object to verify what path is bound to what service code depending on the HTTP method (e.g. POST, GET, etc).
可能有一种更简洁的方法(以及一种更模块化的方式来测试多个路径)但正如我所说,这只是验证路由调用正确服务的基本示例。我正在做的是深入研究koa-router对象,以验证哪个路径绑定到哪个服务代码,具体取决于HTTP方法(例如POST,GET等)。
If you have your routing and your services in modules this test completely avoids dealing with the main koa app. Although technically this test spans multiple units (the routing and the service code) so it would technically be an integration test but it does mean you don't go near app.listen()
which is what you didn't want to call in your tests.
如果您在模块中拥有路由和服务,则此测试完全避免处理主要的koa应用程序。虽然从技术上讲这个测试跨越多个单元(路由和服务代码),所以它在技术上是一个集成测试,但它确实意味着你不要靠近app.listen(),这是你不想在你的试验。