[英]Unit test of authenticateUser - aws-cognito-identity-js - sinon/proxyquire
I am new to node js and testing in general. 我是节点js和测试的新手。 I manage to use sinon to stub my functions etc but now I have to test a function that sends callback depending on event (onSuccess, onFailure). 我设法使用sinon来存储我的函数等但现在我必须测试一个根据事件发送回调的函数(onSuccess,onFailure)。
Here is the code I need to test. 这是我需要测试的代码。
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);
},
});
}
this is what I did so far. 这就是我到目前为止所做的。
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();
}); }); });
This is what I get as error 这就是我得到的错误
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)
It looks like it is going into the onSuccess function and then it does not recognize getIdToken. 看起来它进入onSuccess函数然后它不识别getIdToken。 But it is going too far into the test, no? 但它进入测试太过分了,不是吗? I only want to stub/mock authenticateUser and return a dummy response. 我只想存根/模拟authenticateUser并返回一个虚拟响应。
How can I just tell sinon to give me back a callback on 'onSuccess' without going into the details of the callback? 我怎么能告诉sinon在没有进入回调细节的情况下给我回复'onSuccess'?
Thanks for your help 谢谢你的帮助
You need to pass additional parameters to the callback via yieldsTo
. 您需要通过yieldsTo
将其他参数传递给回调。 eg, 例如,
const getJwtTokenStub = sinon.stub()
const authenticateUserStub = sinon.stub().yieldsTo('onSuccess', {
getIdToken: sinon.stub().returns({getJwtToken: getJwtTokenStub})
});
// then later,
assert.equal(getJwtTokenStub.callCount, 1)
That said, it may not be valuable to have this stuff unit tested. 也就是说,测试这个东西单元可能没什么价值。 You're basically stubbing out a slew of third party functionality and to do what - verify a function gets called? 你基本上是在扼杀一大堆第三方功能并做什么 - 验证一个函数被调用?
Having test coverage for this might be better as an integration test - where you take out the stubs and actually hit aws with credentials specifically used for testing to make sure your app is calling everything correctly. 对此进行测试覆盖可能会更好,作为集成测试 - 您可以取出存根并实际使用专门用于测试的凭据命中aws,以确保您的应用正确调用所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.