Node.js 使用mocha测试节点回调

Node.js 使用mocha测试节点回调,node.js,unit-testing,mocha.js,nodes,aws-lambda,Node.js,Unit Testing,Mocha.js,Nodes,Aws Lambda,我尝试从async.falter中提取方法。我想分别测试它们。下面是在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,

我尝试从
async.falter
中提取方法。我想分别测试它们。下面是在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;
}
测试使用
mocha
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();
      });
    });
  });
});
问题是它从不断言。我正确地进入了方法,但从未进入
lambda.invoke
。似乎mock从未返回回调

  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模拟代码库中尝试一个简单的示例,我不会有任何问题

  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();
    })
  })
也许我没有正确地断言回调的结果


提前感谢您的帮助。

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

我使用aws sdk模拟失败

我最终使用了
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();
    });
  });
});

给出testingaggregate的更多代码,包括lamda是如何定义的。可能与示例不匹配。谢谢@JasonLivesay我已经添加了代码。我自己从来没有使用过chai,但是快速查看API文档告诉我没有
eq
方法,而是
eql
equal
方法()。这意味着您的
expect(err).to.eq('Error invoker!fail')行抛出异常,并且永远不会调用
done()
。感谢@forrert您的编写。我没有成功地改变它。我似乎从来没有进入testingAggregate方法。在模拟中增加测试陷阱中的io写容量。给出testingAggregate的更多代码,包括lamda是如何定义的。可能与示例不匹配。谢谢@JasonLivesay我已经添加了代码。我自己从来没有使用过chai,但是快速查看API文档告诉我没有
eq
方法,而是
eql
equal
方法()。这意味着您的
expect(err).to.eq('Error invoker!fail')行抛出异常,并且永远不会调用
done()
。感谢@forrert您的编写。我没有成功地改变它。我似乎从来没有进入过testingAggregate方法。在模拟中增加测试陷阱中的io写容量。