title: How to test serverless lambda functions with constructors and dependencies?
body: <p>How do I stub out a constructor to pass an expectation on the constructed object that it has received a function call?</p>
  
  <p>I am using serverless, and I have a lambda function which has dependencies and runs every minute via a scheduled event. I want to focus on the behavior of the lambda function so I want one of my tests to be as follows -> it dequeues messages from message-queue. The test will verify that my queue has received a function, dequeueMessages - and that's it. Here's my sample lambda:</p>
  
  <pre><code>module.exports = function(event, context, callback) {
    var queue = new Queue();
  
    queue.dequeueMessages(params).then(messages =&gt; {
      var client = new DataFetcher();
  
      return client.fetchData(messages).then(data =&gt; {
        var database = new Database();
  
        return database.persist(data);
      })
    }
  }
  </code></pre>
  
  <p>I know there are other dependencies there, but I just want to focus on getting the first test to pass, and I am struggling with stubbing out <code>new Queue</code> to make the assertion that the constructed object, queue, has invoked #dequeueMessages. I've explored <a href="http://sinonjs.org/releases/v2.1.0/" rel="nofollow noreferrer">sinon</a> and I have tests set up with mocha and chai, but I just don't know how to put all the tools together to do this very simple test. </p>