Sunday, 30 April 2017

How to test serverless lambda functions with constructors and dependencies?

How do I stub out a constructor to pass an expectation on the constructed object that it has received a function call?

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:

module.exports = function(event, context, callback) {
  var queue = new Queue();

  queue.dequeueMessages(params).then(messages => {
    var client = new DataFetcher();

    return client.fetchData(messages).then(data => {
      var database = new Database();

      return database.persist(data);
    })
  }
}

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 new Queue to make the assertion that the constructed object, queue, has invoked #dequeueMessages. I've explored sinon 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.



via robertjewell

No comments:

Post a Comment