2017-12-16 33 views
2

私はノードjを初めて使用し、一般にテストしています。 私はsinonを使用して関数などをスタブすることができますが、イベント(onSuccess、onFailure)に応じてコールバックを送信する関数をテストする必要があります。authenticateUser - aws-cognito-identity-js-sinon/proxyquireのユニットテスト

ここにテストするコードがあります。

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(); 
    }); }); }); 

これは、私はそれをするonSuccess関数に起こっているし、それがgetIdTokenを認識しないように見えますエラー

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) 

として得るものです。 しかし、それはテストに行き過ぎていますか?私はstub/mock authenticateUserとダミーの応答を返すだけです。

コールバックの詳細を調べなくても、私は 'onSuccess'でコールバックを返すことができますか?あなたの助け

答えて

1

ため

おかげであなたはyieldsTo経由でコールバックに追加のパラメータを渡す必要があります。例:

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

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

つまり、このテストユニットをテストすることは価値がないかもしれません。あなたは基本的にサードパーティ製の機能を突き抜けて何をしているのですか?関数が呼び出されるのを確認しますか?

これをテストするには、スタブを取り出して実際にアプリケーションがすべて正しく呼び出していることを確認するためのテスト用に特別に使用された資格情報を使用して統合テストを行う方がよいでしょう。

+0

ありがとう、私はあなたの解決策を試みます。はい、それは私たちのgetToken関数が何をやろうとしているのかをテストすることです。しかし、あなたが言ったように、統合テストは、資格などでaws-sdkをテストする方が適切です。私はあなたに同意します – peki

関連する問題