功能检查路线是否可进入
function isSessionCookieValid(req, res, next) {
if (!isValid(req.session)) {
return res.status(401).json({
isLoggedIn: false
});
}
return next();
}
在另一个文件中,我使用上面的函数执行一个post请求,它是一个受保护的路由
app
.route('/url')
.post(utils.isSessionCookieValid, (req, res, next) => {})
测试
部分
问题是我不知道如何嘲笑
isSessionCookieValid版本
因为它需要一个next,但无法通过我的测试中的下一个回调:
describe('testing routes', () => {
it('should enter the route body', (done) => {
utils.isSessionCookieValid(req, res, 'next should be here...');
chai.request(server)
.post('/url')
.end(function (error, response, body) {
if (error) {
done(error);
} else {
done();
}
});
});
});
错误
:TypeError:next不是函数