2016-10-31 14 views
1

async.waterfallからメソッドを抽出しようとしています。私はそれらを別々にテストしたいと思います。 AWSでラムダを呼び出す最初のメソッドがあります。ノードのコールバックを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; 
} 

テスト使用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コードベースの簡単な例を試しても、私は問題はありません。

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

lamdaの定義方法を含め、testingaggregateからさらにコードを与えてください。右の例と一致しない可能性があります。 –

+0

ありがとう@JasonLivesay私はコードを追加しました。 – Mio

+0

私はchaiを使ったことは一度もありませんでしたが、APIドキュメントを簡単に見てみると、 'eq'メソッドはなく、むしろ' eql'または 'equal'メソッドです(http://chaijs.com/api/bdd /#method_equal)。これは 'expect(err).to.eq( 'Error invoker!fail');'行が例外をスローし、 'done()'が決して呼び出されないことを意味します。 – forrert

答えて

0

私は私が最終的にproxyquireを使用aws-sdk-mock

を使用して失敗しました。コードはドライではなくクリーンである。私は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(); 
    }); 
    }); 
}); 
関連する問題