[英]Unit testing express.js routes
I am new to express and very new to unit testing.我是新的表达和单元测试的新手。 Consider the following code:
考虑以下代码:
var express = require('express');
var router = express.Router();
var bookingsController = require ("../controllers/bookings");
router
.route('/')
.get(bookingsController.bookings_get)
.post(bookingsController.bookings_post)
router
.route('/:id')
.get(bookingsController.bookings_get_id)
.put(bookingsController.bookings_put_id)
.delete(bookingsController.bookings_delete_id)
module.exports = router;
What is the right/recommended way of writing unit tests for this?为此编写单元测试的正确/推荐方式是什么? I would like to be able to test, for example, that
router.route('/:id')
does not accept POST calls.例如,我希望能够测试
router.route('/:id')
不接受 POST 调用。
I know I can do this with supertest, but I believe that would be considered an integration test as supertest would start the app the run the test.我知道我可以使用 supertest 做到这一点,但我相信这将被视为集成测试,因为 supertest 将启动应用程序并运行测试。
I've read and tried multiple node.js/express.js testing tutorials but couldn't find an answer to this.我已经阅读并尝试了多个 node.js/express.js 测试教程,但找不到答案。 Any pointers to existing tutorials most welcome.
任何指向现有教程的指针都非常受欢迎。 Direct answers here, even more:)
在这里直接回答,甚至更多:)
Here is the unit test solution using sinon.js
to stub express.Router
function and make assertions.这是使用
sinon.js
对express.Router
function 进行 stub 并进行断言的单元测试解决方案。
router.js
: router.js
:
var express = require('express');
var router = express.Router();
var bookingsController = require('./controllers/bookings');
router
.route('/')
.get(bookingsController.bookings_get)
.post(bookingsController.bookings_post);
router
.route('/:id')
.get(bookingsController.bookings_get_id)
.put(bookingsController.bookings_put_id)
.delete(bookingsController.bookings_delete_id);
module.exports = router;
./controller/bookings.js
: ./controller/bookings.js
:
module.exports = {
bookings_get() {},
bookings_post() {},
bookings_get_id() {},
bookings_put_id() {},
bookings_delete_id() {},
};
router.test.js
: router.test.js
:
var sinon = require('sinon');
var express = require('express');
describe('61529619', () => {
it('should pass', () => {
const routerStub = {
route: sinon.stub().returnsThis(),
post: sinon.stub().returnsThis(),
get: sinon.stub().returnsThis(),
put: sinon.stub().returnsThis(),
delete: sinon.stub().returnsThis(),
};
sinon.stub(express, 'Router').callsFake(() => routerStub);
require('./router');
sinon.assert.calledWith(routerStub.route, '/');
sinon.assert.calledWith(routerStub.route, '/:id');
sinon.assert.calledWith(routerStub.get, sinon.match.func);
sinon.assert.calledWith(routerStub.post, sinon.match.func);
sinon.assert.calledWith(routerStub.put, sinon.match.func);
sinon.assert.calledWith(routerStub.delete, sinon.match.func);
});
});
unit test results with 100% coverage:覆盖率 100% 的单元测试结果:
61529619
✓ should pass (869ms)
1 passing (881ms)
----------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 0 | 100 |
61529619 | 100 | 100 | 100 | 100 |
router.js | 100 | 100 | 100 | 100 |
61529619/controllers | 100 | 100 | 0 | 100 |
bookings.js | 100 | 100 | 0 | 100 |
----------------------|---------|----------|---------|---------|------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.