2017-12-16 7 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) 

로 무엇을 얻을 수 있습니다. 하지만 시험에 너무 많이 지나간 것입니까? 난 단지 스텁/모의 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) 

즉,이 물건 단위를 테스트하는 것은 가치가 없을 수 있습니다. 기본적으로 많은 타사 기능을 스텁하고 기능을 호출하는지 확인합니다.

이것에 대한 테스트 적용 범위는 통합 테스트로서 더 낫습니다. 스터브를 꺼내 테스트를 위해 특별히 사용 된 자격 증명으로 실제로 aws를 치는 경우 앱이 모든 것을 올바르게 호출하는지 확인하십시오.

+0

고맙습니다. 해결책을 찾아 보겠습니다. 예, getToken 함수가 수행중인 작업을 수행하는지 테스트하는 것입니다. 하지만 당신이 말했듯이 통합 테스트는 신분증 등으로 aws-sdk를 테스트하는 것이 더 적절합니다. 당신과 동의합니다. – peki