Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js authenticateUser的单元测试-aws cognito identity js-sinon/proxyquire_Node.js_Unit Testing_Sinon_Amazon Cognito_Proxyquire - Fatal编程技术网

Node.js authenticateUser的单元测试-aws cognito identity js-sinon/proxyquire

Node.js authenticateUser的单元测试-aws cognito identity js-sinon/proxyquire,node.js,unit-testing,sinon,amazon-cognito,proxyquire,Node.js,Unit Testing,Sinon,Amazon Cognito,Proxyquire,一般来说,我对NodeJS和测试都是新手。 我设法使用sinon来存根我的函数等,但现在我必须测试一个根据事件(onSuccess、onFailure)发送回调的函数 这是我需要测试的代码 var AWSCognito = require('amazon-cognito-identity-js'); exports.getToken = function (options, callback) { var poolData = { UserPoolId : options.User

一般来说,我对NodeJS和测试都是新手。 我设法使用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)
TypeError:无法读取未定义的属性“getIdToken”
在onSuccess(api/helpers/authentication.js:25:38)
在callCallback(node_modules/sinon/lib/sinon/behavior.js:95:18)
在Object.invoke(node_modules/sinon/lib/sinon/behavior.js:128:9)
在Object.functionStub(node_modules/sinon/lib/sinon/stub.js:98:47)
在Function.invoke(node_modules/sinon/lib/sinon/spy.js:193:47)
在Object.proxy[作为认证者](node_modules/sinon/lib/sinon/spy.js:89:22)
在Object.exports.getToken(api/helpers/authentication.js:23:15)
在上下文中。(test/api/helpers/authenticationTests.js:37:18)
看起来它将进入onSuccess函数,然后它无法识别getIdToken。 但这太过分了,不是吗?我只想存根/模拟authenticateUser并返回一个虚拟响应

我怎么能只告诉sinon在“onSuccess”时给我回电话,而不谈回电话的细节


感谢您的帮助

您需要通过
yieldsTo
向回调传递其他参数。e、 g

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

// then later, 
assert.equal(getJwtTokenStub.callCount, 1)
也就是说,对这些东西进行单元测试可能没有价值。你基本上是在剔除大量的第三方功能,然后去做什么——验证一个函数被调用了


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

谢谢,我将尝试您的解决方案。是的,它是为了测试我们的getToken函数是否正在做它应该做的事情。但正如你所说的,集成测试更适合使用凭据等测试aws sdk。我同意你的看法