title: Mocking in AWS Lambda
body: <p>I have a simple AWS Node.js Lambda, which I would like to test using mocks:</p>
  
  <pre><code>//SimpleLambda.js
  
  var AWS = require('aws-sdk');
  
  exports.handler = function(event, context) {
    var name = getName();
    context.succeed(name);
  };
  
  function getName() {
    return 'David';
  }
  </code></pre>
  
  <p>I've installed <code>mocha</code> and <code>simple-mock</code>, but I am unable to get this to work:</p>
  
  <pre><code>//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);
  
      });
    });
  });
  </code></pre>
  
  <p>Output still suggests <code>getName</code> returns 'David'</p>
  
  <pre><code>  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.&lt;anonymous&gt; (test/test.js:32:14)
  </code></pre>
  
  <p>Can this be done?</p>