lundi 23 novembre 2015

How to mock a method response that returns a promise inside an API endpoint?

I have an endpoint /api/call/:user1/:user2

The way this is defined in my express router is

router.get('/call/:user1/:user2', function(req, res, next) {

  var user1 = req.params.user1,
  user2 = req.params.user2;
  console.log(conf);

  // irrelevant :getting some data
  var options = {
    url : conf.apiServerName + '/api/userinfo/users/ids',
    qs  : {userIds : [user1, user2]}
  };

  //Get from UserInfo Service
  return request.get(options, function(err, resp, body){
      var result = JSON.parse(resp.body);
      var callerMobile = result[callerId + ''].mobileNumber,
          calleeMobile = result[calleeId + ''].mobileNumber;

      if (callerMobile && calleeMobile){

        bridgeCall(user1Mobile, user2Mobile).then(function(data){
          res.json({status : "ok", msg : "Call has been initiated", roomId: data[0].Sid[0]});
        });

      } else{
        res.json({status : "error", msg : "One of the mobile numbers is missing, please check User ID"});
      }

  });
  });

Here the bridgeCall method does some call to a third party API and returns a promise. Is there a way I can mock this response to return some JSON ?

I am using mocha

Aucun commentaire:

Enregistrer un commentaire