如何使用sinon和chai来存根并期望调用此aws-sdk服务



我有这个导出的函数,我想测试sts.assumeRole是否被调用:

const AWS = require('aws-sdk')
const sts = new AWS.STS();
module.exports.assumeRole = async (roleArn) => {
let params = {
RoleArn: roleArn,
RoleSessionName: 'MessagingSession',
}
return await sts.assumeRole(params).promise()
}

这就是我尝试过的:

const chai = require('chai')
const expect = chai.expect
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const AWS = require('aws-sdk')
describe('assumeRole', () => {
it('throws an exception if roleArn is not present', async () => {
const authenticationService = require('../services/authenticationService.js')
await expect(authenticationService.assumeRole('')).to.eventually.be.rejectedWith('no ARN present')
})
it('calls sts assumeRole', () => {
const stsStub = sinon.stub({ assumeRole: () => {} });
const awsStub = sinon.stub(AWS, 'STS');
awsStub.returns(stsStub);
const authenticationService = require('../services/authenticationService.js')
authenticationService.assumeRole('a-role-arn')
expect(stsStub).to.have.been.calledOnce
})
})

这导致TypeError: { assumeRole: [Function: assumeRole] } is not a spy or a call to a spy!

我是间谍、存根和javascript测试的新手,任何帮助都将不胜感激!

注意:我想使用expect断言语法,不想使用任何其他外部包

编辑:现在使用幻灯片2的解决方案,我有以下内容:

const chai = require('chai')
chai.use(require('chai-as-promised'))
const expect = chai.expect
const sinon = require('sinon')
chai.use(require('sinon-chai'))
const AWS = require('aws-sdk')
describe('assumeRole', () => {
it('throws an exception if roleArn is not present', async () => {
const authenticationService = require('../services/authenticationService.js')
await expect(authenticationService.assumeRole('')).to.eventually.be.rejectedWith('no ARN present')
})
it('calls sts.assumeRole', async () => {
const stsFake = {
assumeRole: sinon.stub().returnsThis(),
promise: sinon.stub(),
};
const STSStub = sinon.stub(AWS, 'STS').callsFake(() => stsFake);
const authenticationService = require('../services/authenticationService.js')
await authenticationService.assumeRole('a-role-arn')
sinon.assert.calledOnce(STSStub);
sinon.assert.calledWith(stsFake.assumeRole, { RoleArn: 'a-role-arn', RoleSessionName: 'MessagingSession' });

STSStub.restore();
})
})

这两个测试现在可以独立工作并通过,但如果第二个测试失败,则可以一起运行。我得到错误ValidationError: 1 validation error detected: Value 'a-role-arn' at 'roleArn' failed to satisfy constraint: Member must have length greater than or equal to 20看起来STS库不再被存根化

这是单元测试解决方案:

index.js:

const AWS = require('aws-sdk');
const sts = new AWS.STS();
module.exports.assumeRole = async (roleArn) => {
let params = {
RoleArn: roleArn,
RoleSessionName: 'MessagingSession',
};
return await sts.assumeRole(params).promise();
};

index.test.js:

const sinon = require('sinon');
const AWS = require('aws-sdk');
describe('62918753', () => {
it('should pass', async () => {
const stsFake = {
assumeRole: sinon.stub().returnsThis(),
promise: sinon.stub(),
};
const STSStub = sinon.stub(AWS, 'STS').callsFake(() => stsFake);
const authenticationService = require('./');
await authenticationService.assumeRole('a-role-arn');
sinon.assert.calledOnce(STSStub);
sinon.assert.calledWith(stsFake.assumeRole, { RoleArn: 'a-role-arn', RoleSessionName: 'MessagingSession' });
STSStub.restore();
});
});

带覆盖率报告的单元测试结果:

62918753
✓ should pass (919ms)

1 passing (926ms)
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
index.js |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------

最新更新