Ich bin neu zu Knoten js und Tests im Allgemeinen. Ich kann sinon verwenden, um meine Funktionen usw. zu stubben, aber jetzt muss ich eine Funktion testen, die Callback abhängig vom Ereignis sendet (onSuccess, onFailure).Komponententest des authenticateUser - aws-cognito-identity-js - sinon/proxyquire
Hier ist der Code, den ich testen muss.
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);
},
});
}
Dies ist, was ich bisher gemacht habe.
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();
}); }); });
Dies ist, was ich als Fehler angezeigt
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)
Es sieht aus wie es in die onSuccess Funktion wird und dann ist es nicht getIdToken erkennen. Aber es geht zu weit in den Test, nein? Ich möchte nur authenticateUser stub/mock und eine Dummy-Antwort zurückgeben.
Wie kann ich Sinon einfach sagen, mir einen Rückruf auf 'onSuccess' zurückzugeben, ohne auf die Details des Rückrufs einzugehen?
Danke für Ihre Hilfe
Vielen Dank, ich werde Ihre Lösung versuchen. Ja, es ist zu testen, ob unsere getToken-Funktion das tut, was sie tun soll. Aber wie Sie sagten, ein Integrationstest ist besser geeignet, um die AWS-SDK mit Anmeldeinformationen usw. zu testen. Ich stimme Ihnen zu – peki