【问题标题】:Cannot unit test JavaScript functions with Tape/ Sinon无法使用 Tape/Sinon 对 JavaScript 函数进行单元测试
【发布时间】:2017-06-30 00:26:56
【问题描述】:

我有一个要使用 Tape 和 Sinon 测试的模块。不幸的是,我做得不是很好。这是模块代码:

let config = require('./config');
let request = require('request');
let restify = require('restify');

let certificateUtils = require('utilities');

const validateTheToken = function (token, requestId, callback) {

  const options = {
    url: config.userServiceRootUrl + config.validationPath,
    method: 'POST',
    headers: {
      'token': token,
      'caller': config.callingService,
      'x-request-id': requestId
    }
  };

  if (typeof process.env.CA_STORE !== 'undefined') {
    const certAuth = process.env.CA_STORE + '/trustedCA.pem';
    options.ca = certificateUtils.getAuthorisedCerts(certAuth);
  }

  request(options, function (error, response, body) {
    callback(error, response, body);
  });
};

// add token validation middleware
const authenticateTheToken = function (req, res, next) {
  if (config.enableTokenValidation) {

    const receivedToken = getToken(req);
    if (!receivedToken) {
      return next(new restify.NotAuthorizedError('No token'));
    }

    validateTheToken(receivedToken, req.requestId, function (err, response, body) {
      if (err || response.statusCode != 200) {
        req.logger.error({
          err: err,
          response: response ? {
            statusCode: response.statusCode,
            statusMessage: response.statusMessage,
            body: body
          } : undefined,
        }, 'validation failed');
        return next(new restify.NotAuthorizedError('Not a valid token'));
      } else {
        return next();
      }
    });
  }
  else {
    return next();
  }
};

function getTheToken(req) {


  if (req.headers.token) {
    return req.headers.token;
  } else if (req.headers.user) {
    req.logger.warn({req, user: req.headers.user}, `request was sent with header 'user'`);
    try {
      return JSON.parse(req.headers.user).token;
    } catch (e) {
      req.logger.warn({user: req.headers.user}, `is not valid JSON`);
      return null;
    }
  } else {
    return null;
  }
}

module.exports = {getTheToken, authenticateTheToken};

我如何首先对已调用 authenticateTheToken 进行单元测试?这是我的尝试: test('访问了 authenticateTheToken', function (t) {

 const tokenAuthentication = require('../tokenAuthentication');
 const authenticateToken = tokenAuth.authenticateToken;

 let req = {
    headers: {
        token: 1
     }
 };
 let res = {};
 let next = {};

 let stub = sinon.stub(tokenAuth, 'getToken');

 stub.yields('next');

 authenticateToken(req, res, next);

 t.equal(authenticateToken.callCount, 1);
 t.end();
 });

当我运行测试时,我收到以下错误:

C:\source\my-project\tokenAuthentication.js:40
    req.logger.error({
              ^

TypeError: Cannot read property 'error' of undefined
    at C:\source\my-project\tokenAuthentication.js:40:19
    at Request._callback (C:\source\my-project\tokenAuthentication.js:25:5)
    at self.callback (C:\source\my-project\node_modules\request\request.js:188:22)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Request.init (C:\source\my-project\node_modules\request\request.js:234:17)
    at new Request (C:\source\my-project\node_modules\request\request.js:130:8)
    at request (C:\source\my-project\node_modules\request\index.js:54:10)
    at validateTheToken (C:\source\my-project\tokenAuthentication.js:24:3)
    at authenticateTheToken (C:\source\tokenAuthentication.js:38:5)


npm ERR! Test failed.  See above for more details.

【问题讨论】:

    标签: javascript node.js unit-testing sinon node.js-tape


    【解决方案1】:

    您在这里模拟req,因此您的测试中的req 需要在您的代码中具有req 的所有属性。这将包括记录器。

    req = {
      ...
      logger: {
        warn: () => {},
        error: () => {},
      }
    }
    

    req 可能有很多属性,因此您可能想要创建一个真正的 Request 对象或使用另一个库来模拟 http 请求,例如 nock

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2021-07-28
      • 1970-01-01
      • 2020-08-13
      • 2016-12-21
      • 2019-01-17
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多