代码之家  ›  专栏  ›  技术社区  ›  EugenSunic

节点js使用mocha chai测试受保护的路由?

  •  0
  • EugenSunic  · 技术社区  · 6 年前

    功能检查路线是否可进入

    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不是函数

    0 回复  |  直到 6 年前
    推荐文章