使用承诺的Lambda的sinon存根 [英] sinon stub for Lambda using promises
本文介绍了使用承诺的Lambda的sinon存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始使用sinon,并且在解决DynamoDB调用方面取得了一些初步的成功:
I just started using sinon, and I had some initial success stubbing out DynamoDB calls:
sandbox = sinon.createSandbox()
update_stub = sandbox.stub(AWS.DynamoDB.DocumentClient.prototype, 'update').returns({
promise: () => Promise.resolve(update_meeting_result)
})
这很好.
但是我还需要对Lambda进行存根,并且相同的方法行不通:
But I also need to stub Lambda, and the same approach isn't working:
lambda_stub = sandbox.stub(AWS.Lambda.prototype, 'invoke').returns({
promise: () => Promise.resolve({lambda_invoke_result}) //
})
这样,我得到一个错误:无法对不存在的属性调用进行存根
.
With this, I get the error: Cannot stub non-existent property invoke
.
示例实现:
const AWS = require("aws-sdk")
AWS.config.update({region: 'us-west-2'})
const dynamodb = new AWS.DynamoDB.DocumentClient()
const lambda = new AWS.Lambda()
// lambda function handler
exports.handler = async (event) => {
let result = await dynamodb.get({/* some get config */}).promise()
// do stuff ...
// kick off next lambda
await lambda.invoke({/* lambda config */}).promise()
return {"status": "ok"} // or something
}
推荐答案
这是单元测试解决方案:
Here is the unit test solution:
index.js
:
const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-west-2' });
const dynamodb = new AWS.DynamoDB.DocumentClient();
const lambda = new AWS.Lambda();
exports.handler = async (event) => {
let result = await dynamodb.get({}).promise();
await lambda.invoke({}).promise();
return { status: 'ok' };
};
index.test.js
:
const sinon = require('sinon');
const AWS = require('aws-sdk');
describe('61516053', () => {
afterEach(() => {
sinon.restore();
});
it('should pass', async () => {
const mLambda = { invoke: sinon.stub().returnsThis(), promise: sinon.stub() };
sinon.stub(AWS, 'Lambda').callsFake(() => mLambda);
const mDocumentClient = { get: sinon.stub().returnsThis(), promise: sinon.stub() };
sinon.stub(AWS.DynamoDB, 'DocumentClient').callsFake(() => mDocumentClient);
sinon.stub(AWS.config, 'update');
const { handler } = require('./');
await handler();
sinon.assert.calledWith(AWS.config.update, { region: 'us-west-2' });
sinon.assert.calledOnce(AWS.DynamoDB.DocumentClient);
sinon.assert.calledOnce(AWS.Lambda);
sinon.assert.calledWith(mLambda.invoke, {});
sinon.assert.calledOnce(mLambda.promise);
sinon.assert.calledWith(mDocumentClient.get, {});
sinon.assert.calledOnce(mDocumentClient.promise);
});
});
具有100%覆盖率的单元测试结果:
unit test results with 100% coverage:
61516053
✓ should pass (907ms)
1 passing (915ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
这篇关于使用承诺的Lambda的sinon存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文