title: Testing a function with request call in Mocha
body: <p>I am very new to node.js and Mocha. I want to test the following function:</p>
  
  <pre><code>querybackend = function(url,queryParams) {
      var backendData ={};
      request({url:url, qs:queryParams}, function(err, response, body) {
          if(err) { console.log(err); return; }
          var data = JSON.parse(body);
          var length = data.length;
          var tmp = data[0].DataPoints[0].length;
          var in_sum =Array.apply(null, new Array(tmp)).map(Number.prototype.valueOf,0);
          var timestamp = [];
          var index1;
          var index2;
          for(index1=0;index1 &lt; length;index1++) {
              var length2=data[index1].DataPoints.length;
              for(index2=0;index2&lt;length2;index2++) {
                  in_sum[index2]= in_sum[index2] + data[index1].DataPoints[index2][1];
                  timestamp[index2] = data[index1].DataPoints[index2][0];
              }
          }
          backendData.count = in_sum;
          backendData.timestamp  = timestamp;
          deferred.resolve(backendData);
      });
      return deferred.promise;
  };
  </code></pre>
  
  <p>What is the correct way to test this function? This function hits a rest end-point, gets a JSON, parses it and returns an object. </p>
  
  <p>This is a sample JSON which I get:</p>
  
  <pre><code>[
      {
          "Name": "name1",
          "DataPoints": [
              [
                  1410627915,
                  242221
              ]
          ]
      },
      {
          "Name": "name2",
          "DataPoints": [
              [
                  1410627913,
                  238157
              ]
          ]
      },
      {
          "Name": "name3",
          "DataPoints": [
              [
                  1410627922,
                  215045
              ]
          ]
      }
  ] 
  </code></pre>
  
  <p>Should I mock the request? How do I mock it in Mocha? </p>