mardi 5 mai 2015

stubbing api request in async series

I have a module which resizes an image, imageResizer, using graphicsmagick and another module, apiCaller, which has two functions, one for a get request and another for a post request, using the request module. In imageResizer I call apiCaller.get, a function which triggers the image resizing is then called if the get request is successful and then if that goes alright apiCaller.post is called.

It is somethings like this:

exports.imageResizer = function (arguments) {

    async.series([
      function(callback) {
         apiCaller.get(callback);
      },
      function(callback) {
         gm("filepath")
          .resize("sizes")
          .write("destinationPath", function (err) {
            if(err) // do something
          }, callback());
      },
      function(callback) {
         apiCallerPost(arguments)
      }
    ]);
}


apiCaller = {};

apiCaller.get = function (arguments, callback) {
   // setup variables and stuff
   function _callback (error, response, body) {
     if (error) {// do somethind}
     else {callback(null, body}
   }

   request.get(options, _callback);
}

module.exports = apiCaller;

My question his which would be the best approach to testing this? Would stubbing apiCaller.get and apiCaller.post be the best approach? What about the request module itself? Would that need to be stubbed too?

I am using sinon, chai, mocha and proxyquire.

Aucun commentaire:

Enregistrer un commentaire