dimanche 20 mars 2016

Mocking in AWS 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';
}

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

    });
  });
});

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)

Can this be done?

Aucun commentaire:

Enregistrer un commentaire