我正在尝试对Express路由进行单元测试,以验证它们是否调用了正确的控制器功能。此时,我没有检查控制器功能的内部逻辑,只是路由映射到正确的功能。到目前为止我所做的工作完全正常,直到我介绍路由器中间件,如此:
'use strict';
import express from 'express';
import config from './../../config/environments';
import SuperLogin from 'superlogin';
let controller = require('./subjects.controller');
let router = express.Router();
let superlogin = new SuperLogin(config);
router.get('/', superlogin.requireAuth, superlogin.requireAnyRole(['admin', 'specialist', 'nurse']), controller.index);
router.get('/:subjectId', superlogin.requireAuth, superlogin.requireAnyRole(['admin', 'specialist', 'nurse']), controller.show);
router.post('/', superlogin.requireAuth, superlogin.requireAnyRole(['admin', 'specialist', 'nurse']), controller.create);
router.put('/:subjectId', superlogin.requireAuth, superlogin.requireAnyRole(['admin', 'specialist', 'nurse']), controller.upsert);
router.patch('/:subjectId', superlogin.requireAuth, superlogin.requireAnyRole(['admin', 'specialist', 'nurse']), controller.patch);
router.delete('/:subjectId', superlogin.requireAuth, superlogin.requireAnyRole(['admin']), controller.destroy);
module.exports = router;
如您所见,我正在使用Superlogin来验证对每条路线的访问权限。如果我删除了我的测试通过,添加中间件会导致它们失败。我可能需要存根superlogin方法,但我不知道在哪里这样做。
我的测试如下
'use strict';
/* globals sinon, describe, expect, it */
let proxyquire = require('proxyquire').noPreserveCache();
let subjectCtrlStub = {
index: 'subjectCtrl.index',
show: 'subjectCtrl.show',
create: 'subjectCtrl.create',
upsert: 'subjectCtrl.upsert',
patch: 'subjectCtrl.patch',
destroy: 'subjectCtrl.destroy'
};
sinon.stub(superlogin, 'requireAuth', function(req, res, next) {
return next();
});
let routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
let subjectRoutes = proxyquire('./subjects.routes.js', {
express: {
Router() {
return routerStub;
}
},
'./subjects.controller': subjectCtrlStub
});
describe('Subject API Router:', function() {
it('should return an express router instance', function() {
subjectRoutes.should.equal(routerStub);
});
describe('GET /api/subjects', function() {
it('should route to subjects.controller.index', function() {
routerStub.get
.withArgs('/', 'subjectCtrl.index')
.should.have.been.calledOnce;
});
});
describe('GET /api/subjects/:subjectId', function() {
it('should route to subjects.controller.show', function() {
routerStub.get
.withArgs('/:subjectId', 'subjectCtrl.show')
.should.have.been.calledOnce;
});
});
describe('POST /api/subjects', function() {
it('should route to subjects.controller.create', function() {
routerStub.post
.withArgs('/', 'subjectCtrl.create')
.should.have.been.calledOnce;
});
});
describe('PUT /api/subjects/:subjectId', function() {
it('should route to subjects.controller.upsert', function() {
routerStub.put
.withArgs('/:subjectId', 'subjectCtrl.upsert')
.should.have.been.calledOnce;
});
});
describe('PATCH /api/subjects/:subjectId', function() {
it('should route to subjects.controller.patch', function() {
routerStub.patch
.withArgs('/:subjectId', 'subjectCtrl.patch')
.should.have.been.calledOnce;
});
});
describe('DELETE /api/subjects/:subjectId', function() {
it('should route to subjects.controller.destroy', function() {
routerStub.delete
.withArgs('/:subjectId', 'subjectCtrl.destroy')
.should.have.been.calledOnce;
});
});
});
我有一个在所有这些测试之前运行的before函数,它使一些具有不同角色的示例用户为数据库播种,并将授权令牌保存到全局变量,以便在测试端点时使用,我只是不知道如何使用routerStub函数调用发送它们。
答案 0 :(得分:0)
使用stubs为已知值的SUT提供直接或不正确的输入。
您可能会考虑事实的证实,在某些情况下,路由可以从路由到达什么。在这种情况下,使用存根为中间件提供影响其行为的输入数据(例如用于身份验证的用户凭据)。不要用存根替换中间件本身,因为如果存根完全改变了行为,那么这种测试将无关紧要。