Ich versuche, Methode von einem async.waterfall
zu extrahieren. Ich möchte sie getrennt testen. Hier ist die erste Methode, die auf AWS ein Lambda aufruft.Testen von Knoten Rückrufe mit Mocha
const async = require('async');
const AWS = require('aws-sdk');
AWS.config.region = 'eu-west-1';
const lambda = new AWS.Lambda();
const table_name = 'my_table';
exports.handler = function(event, context) {
async.waterfall([
increase_io_write_capacity(callback)
],
function (err) {
if (err) {
context.fail(err);
} else {
context.succeed('Succeed');
}
});
};
function increase_io_write_capacity(callback) {
var payload = JSON.stringify({
tableName:table_name,
increaseConsumedWriteCapacityUnits: 5
});
var params = {
FunctionName: 'dynamodb_scaling_locker',
InvocationType: 'RequestResponse',
Payload: payload
};
lambda.invoke(params, function(error, data) {
if (error) {
console.log('Error invoker!' + JSON.stringify(error));
callback('Invoker error' + JSON.stringify(error));
} else {
console.log('Done invoker!' + JSON.stringify(data));
callback(null);
}
});
}
if (typeof exports !== 'undefined') {
exports.increase_io_write_capacity = increase_io_write_capacity;
}
Der Test Verwendung mocha
und aws-sdk-mock
.
const aws = require('aws-sdk-mock');
const testingAggregate = require('../index.js');
const assert = require('assert');
const expect = require('chai').expect;
const event = '';
describe('Testing aggregate function', function() {
afterEach(function (done) {
aws.restore();
done();
});
describe('increase_io_write_capacity', function() {
it('fail accessing to the lambda', function(done){
aws.mock('Lambda', 'invoke', function(params, callback){
callback('fail', null);
});
testingAggregate.increase_io_write_capacity(function(err, data){
expect(err).to.equal('Error invoker! fail');
done();
});
});
});
});
Das Problem ist es nie behaupten. Ich gehe richtig in die Methode, aber nie in die . Es scheint, dass der Mock den Rückruf nie zurückgibt.
Testing aggregate function
increase_io_write_capacity
1) fail accessing to the lambda
0 passing (2s)
1 failing
1) Testing aggregate function increase_io_write_capacity fail accessing to the lambda:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
Wenn ich ein einfaches Beispiel in aws-sdk-mock Codebasis versuche, habe ich kein Problem.
t.test('mock function replaces method with replace function with lambda', function(st){
awsMock.mock('Lambda', 'invoke', function(params, callback){
callback("error", null);
});
var lambda = new AWS.Lambda();
lambda.invoke({}, function(err, data){
st.equals(err, "error'");
awsMock.restore('Lambda');
st.end();
})
})
Vielleicht behaupte ich nicht ordnungsgemäß das Ergebnis des Rückrufs?
Vielen Dank im Voraus für Ihre Hilfe.
Geben Sie mehr Code von testingaggregate einschließlich, wie Lamda dort definiert ist. Möglicherweise stimmt das Beispiel nicht überein. –
Danke @JasonLivesay Ich habe den Code hinzugefügt. – Mio
Ich habe Chai nie selbst benutzt, aber ein kurzer Blick auf das API doc sagt mir, dass es keine 'eq' Methode gibt, sondern eine' eql' oder 'equal' Methode (http://chaijs.com/api/bdd/# method_equal). Das würde bedeuten, dass Ihre 'expect (err) .to.eq ('Error invoker! Fail');' Zeile eine Ausnahme auslöst und 'done()' niemals aufgerufen wird. – forrert