【问题标题】:ExpressJS: How to mock request handler next function with mochaExpressJS:如何使用 mocha 模拟请求处理程序下一个函数
【发布时间】:2015-06-24 09:55:58
【问题描述】:

我有一个处理程序

public ensureAuthenticated(req: express.Request, res: express.Response, next: Function) {
    // check header or url parameters or post parameters for token
      var token = req.body.token || req.param('token') || req.headers['x-access-token'];
    // decode token
    if (token) {
        // verifies secret and checks exp
        jwt.verify(token, config.secret, function(err, decoded) {   
            if (err) {
                return res.status(404).json({ success: false, message: 'Failed to authenticate token.' });      
            } else {
                // if everything is good, save to request for use in other routes
                next();
            }
        });

    } else {
        // if there is no token
        // return an error
        return res.status(403).send({ 
            success: false, 
            message: 'No token provided.'
        });
    }
  }

这里是路线

app.post('/api/article/create', AuthenticationHelper.ensureAuthenticated, this.create);

在单元测试中,我如何模拟 ensureAuthenticated 以确保它已通过身份验证。

sinon.stub(AuthenticationHelper, 'ensureAuthenticated').returns(true);

【问题讨论】:

    标签: node.js express mocha.js


    【解决方案1】:

    我给你一个例子,我在不使用 sinon 的情况下测试它。

    这是我的 authentication-helper.js:

    'use strict';
    
    module.exports = function(jwt, config) {
      return {
        ensureAuthenticated: function (req, res, next) {
          var token = req.body.token ||
                    req.param('token') ||
                    req.headers['x-access-token'];
    
          if (token) {
            jwt.verify(
                  token,
                  config.secret,
                  function(err, decoded) {
                    if (err) {
                      res
                      .status(404)
                      .json({
                        success: false,
                        message: 'Failed to auth.'
                      });
                    } else {
                      next();
                    }
                  }
            );
          } else {
            res
            .status(403)
            .send({
              success: false,
              message: 'No token provided.'
            });
          }
        }
      };
    }
    

    这是我的测试文件:

    'use strict';
    
    var jwt = {};
    jwt.verify = function (token, secret, fn) {
      fn(null, 'something');
    };
    
    var config = {};
    config.secret = 'shh';
    
    var req = {};
    req.body = {};
    req.body.token = 'mytoken';
    
    var res = {};
    
    var AuthenticationHelper = require('./authentication-helper.js')(jwt, config);
    
    describe('Test Express Middleware', function() {
      it('should call next on middlware', function(done) {
        var next = function () {
          console.log('next was called');
          done();
        };
    
        AuthenticationHelper.ensureAuthenticated(req, res, next);
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-14
      • 2012-06-24
      • 1970-01-01
      • 2021-10-26
      • 2014-05-06
      • 2022-01-21
      • 1970-01-01
      • 2014-02-10
      相关资源
      最近更新 更多