Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

aws sdk nodejs - sinon stub and expect a call to this aws-sdk service using sinon and chai

I am new to Sinon stub for unit testing. I tried to work with the following code but getting errors (TypeError: sts.assumeRole is not a function). Please note that i can't change my source code to make my test case pass. Please suggest.

index.js

async function getcred(rolearn) {
    console.log("***********  getcred *********************");
    return new Promise((resolve, reject) => {
        const timestamp = (new Date()).getTime();
        const params = {
            RoleArn: rolearn,
            RoleSessionName: `session-${timestamp}`,
            DurationSeconds: 3600
        };
        sts.assumeRole(params, (err, data) => {
            if (err)
                reject(err);
            else {
                resolve({
                    accessKeyId: data.Credentials.AccessKeyId,
                    secretAccessKey: data.Credentials.SecretAccessKey,
                    sessionToken: data.Credentials.SessionToken,
                });
            }
        });
    });
}

test case:

const sinon = require('sinon');
const AWS = require('aws-sdk');
//var index = require('../index');

describe('62918753', () => {
  it('should pass', async () => {
    const stsFake = {
      getCrossAccountCredentials: sinon.stub().returnsThis()
      promise: sinon.stub().resolve('mock data'),
    };
    const STSStub = sinon.stub(AWS, 'STS').callsFake(() => stsFake);
    const authenticationService = require('../index');
    await authenticationService.getCrossAccountCredentials('a-role-arn');
    sinon.assert.calledOnce(STSStub);
    sinon.assert.calledWith(stsFake.getCrossAccountCredentials, { RoleArn: 'a-role-arn', RoleSessionName: 'MessagingSession' });
    STSStub.restore();
  });
});
question from:https://stackoverflow.com/questions/65923440/sinon-stub-and-expect-a-call-to-this-aws-sdk-service-using-sinon-and-chai

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...