本文介绍了在AWS Lambda中进行模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的AWS Node.js Lambda,我想使用模拟对其进行测试:
I have a simple AWS Node.js Lambda, which I would like to test using mocks:
//SimpleLambda.js
var AWS = require('aws-sdk');
exports.handler = function(event, context) {
var name = getName();
context.succeed(name);
};
function getName() {
return 'David';
}
我已经安装了mocha
和simple-mock
,但是我无法使其正常工作:
I've installed mocha
and simple-mock
, but I am unable to get this to work:
//test.js
//Mocha
var assert = require('assert');
//Chai
var chai = require('chai');
var expect = chai.expect;
var should = chai.should();
//Simple-Mock
var simple = require('simple-mock');
//Lambda
var lambda = require('../SimpleLambda');
describe('SimpleLambda tests', function() {
describe('Get name', function() {
beforeEach(function() {
simple.mock(lambda, 'getName');
});
it('should return \'Tim\' when the mocked with simple-mock', function() {
lambda.getName.returnWith('Tim');
var context = {
invokeid: 'invokeid',
succeed: function(result) {
expect(result).to.equal("Tim");
return result;
}
};
lambda.handler({}, context);
});
});
});
输出仍然表明getName
返回'David'
Output still suggests getName
returns 'David'
SimpleLambda tests
Get name
1) should return 'Tim' when the mocked with simple-mock
0 passing (11ms)
1 failing
1) SimpleLambda tests Get name should return 'Tim' when the mocked with simple-mock:
AssertionError: expected 'Succesfully retrieved: David' to equal 'Tim'
+ expected - actual
-Succesfully retrieved: David
+Tim
at Object.context.succeed (test/test.js:27:29)
at Object.exports.handler (SimpleLambda.js:5:11)
at Context.<anonymous> (test/test.js:32:14)
可以做到吗?
推荐答案
您需要导出您的getName
函数,以便可以从test.js对其进行访问(并可以由您的模拟库包装).
You need to export your getName
function so that it's accessible from test.js (and can be wrapped by your mocking library).
类似这样的东西:
//SimpleLambda.js
var AWS = require('aws-sdk');
exports.handler = function(event, context) {
var name = exports.getName();
context.succeed(name);
};
exports.getName = function (){
return 'David';
}
这篇关于在AWS Lambda中进行模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!