2016-10-31 160 views
1

我尝试从async.waterfall中提取方法。我想单独测试它们。这是第一个在AWS上调用lambda的方法。使用摩卡测试节点回调

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; 
} 

测试使用mochaaws-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(); 
     }); 
    }); 
    }); 
}); 

问题是它从不断言。我正确地进入该方法但从未进入lambda.invoke。看起来这个模拟器永远不会回复这个回调。

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. 

如果我尝试aws-sdk-mock codebase中的一个简单示例,我没有任何问题。

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(); 
    }) 
    }) 

也许我没有妥善断言回调的结果?

在此先感谢您的帮助。

+0

从testingaggregate包括LAMDA是如何定义的有给更多的代码。可能与示例不正确。 –

+0

谢谢@JasonLivesay我已经添加了代码。 – Mio

+0

我自己从来没有用过柴,但快速浏览一下API文档告诉我,没有'eq'方法,而是'eql'或'equal'方法(http://chaijs.com/api/bdd /#method_equal)。这意味着你的expect(err).to.eq('Error invoker!fail');'行会抛出异常,并且'done()'永远不会被调用。 – forrert

回答

0

我使用aws-sdk-mock

我终于用proxyquire失败。代码不是那么干净,不干。我不是JS开发者。但它的工作。随意进行更改。

const proxyquire = require('proxyquire').noCallThru(); 
const expect = require('chai').expect; 

describe('testingAggregate', function() { 
    describe('increase_io_write_capacity', function() { 
    it('fail invoke the lambda', function(done){ 
     let awsSdkMock = { 
     config: { 
      region: 'eu-west-1' 
     }, 
     Lambda: function() { 
      this.invoke = function(params, callback) { 
      expect(params.Payload).to.eq('{"tableName": "my_table_name","increaseConsumedWriteCapacityUnits":5}'); 
      callback(new Error('Lambda not in this region')); 
      }; 
     } 
     }; 
     let testingAggregate = proxyquire('../index', { 'aws-sdk': awsSdkMock }); 

     testingAggregate.increase_io_write_capacity(function(err, data){ 
     expect(err).to.equal('Invoker error: Error: Lambda not in this region'); 
     }); 
     done(); 
    }); 

    it('succeed invoking the lambda', function(done){ 
     let awsSdkMock = { 
     config: { 
      region: 'eu-west-1' 
     }, 
     Lambda: function() { 
      this.invoke = function(params, callback) { 
      callback(null); 
      }; 
     } 
     }; 
     let testingAggregate = proxyquire('../index', { 'aws-sdk': awsSdkMock }); 

     testingAggregate.increase_io_write_capacity(function(err, data){ 
     expect(err).to.equal(null); //really not the best test 
     }); 
     done(); 
    }); 
    }); 
});