如何使用 sinon 和 chai 存根并期望调用此 aws-sdk 服务
how to stub and expect a call to this aws-sdk service using sinon and chai
我有这个导出函数,我想测试 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
断言语法,不想使用任何其他外部包
编辑:现在使用 slideshowp2 的解决方案,我有以下内容:
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 |
----------|---------|----------|---------|---------|-------------------
我有这个导出函数,我想测试 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
断言语法,不想使用任何其他外部包
编辑:现在使用 slideshowp2 的解决方案,我有以下内容:
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 |
----------|---------|----------|---------|---------|-------------------