samedi 2 mai 2015

testing external api calls in node.js using mocha.js

I'm trying to write tests for my npm module, which takes care of communicating with my backend api. this module will sit inside a cordova android app, and will take care of any api calls. the issue that i'm having seems to be an understanding of mocha, but i've had a good look around the internet and can't find a solution so i turn to the masses. As an example, i have a function like

  test: function() {

    request.get({
      url: defaultHost,
      headers: {
      }
    }, function(err, httpResponse, body) {

      if(err) {
        return err;
      } else {
        console.log(body);
        return body;
      }
    });
  }

this works will. i'm now trying to create the test for it in mocha. the problem that i'm getting is that i have no idea how to get the return function from the .get call into the mocha test. the api returns json, so i know that i'm going to have to be doing an is equal comparison, but at the moment i can't even get it to print the results. i think the problem with is that with my other mocha tests that i can get working, they all have an argument that you pass in where as this doesn't. my current code for the test looks like this.

describe('#test', function() {
  it('tests api reachability;', function() {
    var test = new test();
  });
});

if someone can explain what is required afterwards or even just point me in the right direction on google, that would be awesome. i'm normally pretty good at the google, but failing to find direction on this one.

Aucun commentaire:

Enregistrer un commentaire