2

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.

1 Answer 1

1

I think nock will solve this issue. Let's assume you sending get request to some resource (http://domain.com/resource.json) and the tests would be like this:

var nock   = require('nock');

// ... 

describe('#test', function() {
  beforeEach(function () {
    nock('http://domain.com')
      .get('resource.json')
      .reply(200, {
        message: 'some message'
      });
  });

  it('tests api reachability;', function() {
    var test = new test();
  });
});
Sign up to request clarification or add additional context in comments.

3 Comments

I don't actually require a mock on calling the domain. it's already calling the object in the function i want to test, and i would consider this to be part of the actual test anyway. all i want is results of the ' if(err) { return err; } else { console.log(body); return body; }' to be accessible in the test script.
Actualy the nock will not mock the request, it will mock the external response. And to access to the result you need to pass callback to the test function, because request will work asynchronously.
this isn't quite what i'm looking for. as i've dug deeper into it, i've realised that i think the problem is due to the function completing before the async request returns a response. the function is returning true because the block in the centre isn't actually executing until after the function it's wrapped in completes anyway. if i try to put the return method in the request response block, the function returns nil, which causes the test to fail. i want to include testing for api connectivity and do not want to mock the request or the external response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.