找到你要的答案

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

Q:测试外部API调用js使用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.

我想我的NPM模块编写测试,它负责与我的后端API。本模块将坐在科尔多瓦的Android应用程序,并将照顾任何API调用。我似乎是摩卡的理解问题,但我已经在网上好好看,不能找到一个解决方案,所以我把群众。作为一个例子,我有一个函数

  test: function() {

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

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

这作品将。我现在想在摩卡的创建测试。我的问题是我不知道如何从得到的回报函数,得到打电话到摩卡试验。API返回的JSON,因此我知道我要做一个平等的比较,但现在我甚至不能打印结果。我认为这个问题是与我的其他摩卡测试,我能得到工作,他们都有一个说法,你通过在这没有我的当前代码的测试看起来像这样。

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

如果有人可以解释什么是必需的后,甚至只是指出我在正确的方向上谷歌,这将是真棒。我通常很好的谷歌,但未能找到这个方向。

answer1: 回答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();
  });
});

我认为诺克将解决这个问题。让我们假设你发送GET请求一些资源(http://domain.com/resource.json)和测试会是这样:

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();
  });
});
node.js  testing  mocha  chai