dee*_*mvp 5 amazon-s3 node.js sinon chai sinon-chai
我正在尝试编写单元测试,但在存根 AWS S3 getobject 方法时遇到问题。这是我的代码:
describe('testExecuteSuccess()', function () {
it('Test execution with id is successful.', async () => {
let id = "test_id";
const getObjectStub = AWS.S3.prototype.getObject = sinon.stub();
getObjectStub.returns({ promise: () => Promise.resolve({ Body: "test" }) });
await executionHandler.execute(id).then(() => {
getObjectStub.should.have.been.calledOnce;
});
});
});
Run Code Online (Sandbox Code Playgroud)
有谁知道它有什么问题/如何正确存根 getObject 方法?当我运行测试时,我得到InvalidParameterType: Expected params.Bucket to be a string证明存根不起作用。
这是 executionHandler.execute 方法的代码:
exports.execute = async function(curr_id) {
let params = { Bucket: BUCKET_NAME, Key: KEY_PATH }
let fileObject = await s3.getObject(params).promise();
let codeId = executeCode(fileObject).id;
if (codeId !== curr_id) {
throw "Id from executed code does not match currentId: " + curr_id;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用 Mocha 测试运行器。
您可以消除依赖关系,例如带有链接接缝的aws-sdk模块。这是 CommonJS 版本,因此我们将使用proxyquire to construct our seams.
单元测试解决方案:
\nmain.js:
const AWS = require(\'aws-sdk\');\nconst s3 = new AWS.S3();\n\nexports.execute = async function (curr_id) {\n const BUCKET_NAME = \'examplebucket\';\n const KEY_PATH = \'SampleFile.txt\';\n let params = { Bucket: BUCKET_NAME, Key: KEY_PATH };\n let fileObject = await s3.getObject(params).promise();\n let codeId = executeCode(fileObject).id;\n if (codeId !== curr_id) {\n throw \'Id from executed code does not match currentId: \' + curr_id;\n }\n};\n\nfunction executeCode(file) {\n return file;\n}\nRun Code Online (Sandbox Code Playgroud)\nmain.test.js:
const proxyquire = require(\'proxyquire\');\nconst sinon = require(\'sinon\');\nconst chai = require(\'chai\');\nconst chaiAsPromised = require(\'chai-as-promised\');\n\nchai.use(chaiAsPromised);\nconst { expect } = chai;\n\ndescribe(\'51959840\', () => {\n afterEach(() => {\n sinon.restore();\n });\n it(\'should do nothing if codeId is equal with curr_id\', async () => {\n const fileObject = { id: \'1\' };\n const s3Mock = {\n getObject: sinon.stub().returnsThis(),\n promise: sinon.stub().resolves(fileObject),\n };\n const AWSMock = { S3: sinon.stub().returns(s3Mock) };\n const { execute } = proxyquire(\'./main\', {\n \'aws-sdk\': AWSMock,\n });\n await execute(\'1\');\n sinon.assert.calledOnce(AWSMock.S3);\n sinon.assert.calledWithExactly(s3Mock.getObject, { Bucket: \'examplebucket\', Key: \'SampleFile.txt\' });\n sinon.assert.calledOnce(s3Mock.promise);\n });\n\n it(\'should throw error if codeId is NOT equal with curr_id\', async () => {\n const fileObject = { id: \'2\' };\n const s3Mock = {\n getObject: sinon.stub().returnsThis(),\n promise: sinon.stub().resolves(fileObject),\n };\n const AWSMock = { S3: sinon.stub().returns(s3Mock) };\n const { execute } = proxyquire(\'./main\', {\n \'aws-sdk\': AWSMock,\n });\n await expect(execute(\'1\')).to.rejectedWith(\'Id from executed code does not match currentId: 1\');\n sinon.assert.calledOnce(AWSMock.S3);\n sinon.assert.calledWithExactly(s3Mock.getObject, { Bucket: \'examplebucket\', Key: \'SampleFile.txt\' });\n sinon.assert.calledOnce(s3Mock.promise);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n单元测试结果:
\n 51959840\n \xe2\x9c\x93 should do nothing if codeId is equal with curr_id (2884ms)\n \xe2\x9c\x93 should throw error if codeId is NOT equal with curr_id\n\n\n 2 passing (3s)\n\n----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\nAll files | 100 | 100 | 100 | 100 | \n main.js | 100 | 100 | 100 | 100 | \n----------|---------|----------|---------|---------|-------------------\nRun Code Online (Sandbox Code Playgroud)\n软件包版本:
\n 51959840\n \xe2\x9c\x93 should do nothing if codeId is equal with curr_id (2884ms)\n \xe2\x9c\x93 should throw error if codeId is NOT equal with curr_id\n\n\n 2 passing (3s)\n\n----------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n----------|---------|----------|---------|---------|-------------------\nAll files | 100 | 100 | 100 | 100 | \n main.js | 100 | 100 | 100 | 100 | \n----------|---------|----------|---------|---------|-------------------\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1008 次 |
| 最近记录: |