最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

authenticateUser的单元测试

运维笔记admin7浏览0评论

authenticateUser的单元测试

authenticateUser的单元测试

我是节点js和测试的新手。我设法使用sinon来存储我的函数等但现在我必须测试一个根据事件发送回调的函数(onSuccess,onFailure)。

这是我需要测试的代码。

var AWSCognito = require('amazon-cognito-identity-js');

exports.getToken = function (options, callback) {
  var poolData = {
    UserPoolId : options.UserPoolId,
    ClientId : options.ClientId,
  };
  var authenticationData = {
    Username : options.username,
    Password : options.password,
  };
  var userPool = new AWSCognito.CognitoUserPool(poolData);
  var authenticationDetails = new AWSCognito.AuthenticationDetails(authenticationData);
  var userData = {
    Username : options.username,
    Pool : userPool
  };

  var cognitoUser = new AWSCognito.CognitoUser(userData);

  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
      callback(null, {idToken: result.getIdToken().getJwtToken()});
    },
    onFailure: function (err) {
      callback(err);
    },
  });
}

这就是我到目前为止所做的。

var proxyquire = require('proxyquire'); var should = require('should'); var sinon = require('sinon'); var AWSCognito = require('amazon-cognito-identity-js');

describe('authentication tests', function () {   var expectedResult;

  it('should invoke a lambda function correctly', function (done) {
    var options = {
      username: 'username1',
      password: 'pwd',
      UserPoolId : 'user_Pool',
      ClientId : 'clientId'
    };
    var expectedResult = {
      idToken: '123u'
    };

    var authenticateUserStub = sinon.stub().yieldsTo('onSuccess');

    var testedModule = proxyquire('../../../api/helpers/authentication.js', {
      'amazon-cognito-identity-js': {
        'CognitoUser': function () {
          return {
            authenticateUser: authenticateUserStub
          }
        }
      }
    });

    testedModule.getToken(options, function (err, data) {
      // should.not.exist(err);
      // data.should.eql(expectedResult);
      done();
    });   }); });

这就是我得到的错误

TypeError: Cannot read property 'getIdToken' of undefined
    at onSuccess (api/helpers/authentication.js:25:38)
    at callCallback (node_modules/sinon/lib/sinon/behavior.js:95:18)
    at Object.invoke (node_modules/sinon/lib/sinon/behavior.js:128:9)
    at Object.functionStub (node_modules/sinon/lib/sinon/stub.js:98:47)
    at Function.invoke (node_modules/sinon/lib/sinon/spy.js:193:47)
    at Object.proxy [as authenticateUser] (node_modules/sinon/lib/sinon/spy.js:89:22)
    at Object.exports.getToken (api/helpers/authentication.js:23:15)
    at Context.<anonymous> (test/api/helpers/authenticationTests.js:37:18)

看起来它进入onSuccess函数然后它不识别getIdToken。但它进入测试太过分了,不是吗?我只想存根/模拟authenticateUser并返回一个虚拟响应。

我怎么能告诉sinon在没有进入回调细节的情况下给我回复'onSuccess'?

谢谢你的帮助

回答如下:

您需要通过yieldsTo将其他参数传递给回调。例如。,

const getJwtTokenStub = sinon.stub()
const authenticateUserStub = sinon.stub().yieldsTo('onSuccess', {
  getIdToken: sinon.stub().returns({getJwtToken: getJwtTokenStub})
});

// then later, 
assert.equal(getJwtTokenStub.callCount, 1)

也就是说,测试这个东西单元可能没什么价值。你基本上是在扼杀一大堆第三方功能并做什么 - 验证一个函数被调用?

对此进行测试覆盖可能会更好,作为集成测试 - 您可以取出存根并实际使用专门用于测试的凭据命中aws,以确保您的应用正确调用所有内容。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论