测试该路由有回调的Express路由

时间:2016-09-16 22:42:20

标签: express callback nock

我想模拟一个调用api的快速路由的curl请求。我已经找到了很多关于如何执行此操作的文档,但由于我的代码中有回调,因此我遇到了问题。

var request = require('request')

function queryConsul(req, res) {
  var options = {
      url: 'http://10.244.68.3:8500/v1/catalog/node/services'
  };

  request(options, callback)

  function callback(error, response, body) {
    console.log("hola?");
    if (!error && response.statusCode == 200) {
      response=body
    }
    res.send(response)
  }
}

module.exports = queryConsul;

当我运行当前测试时,我得到一个错误:超时200ms

这是我的测试,我已经尝试使用nock作为存根服务,任何帮助都将非常感谢!!!

var queryConsul = require("../../../helper/queryConsulService");
var expect = require("chai").expect;
var nock = require("nock");

describe("Consul API Queries", () => {
  beforeEach(() => {
    var consulResponse =
    {
     "Node": {
     "Node": "Services",
     "Address": "some_address",
     "TaggedAddresses": null,
     "CreateIndex": 72389,
     "ModifyIndex": 72819
 },
 "Services": {
     "OneBitesTheDust": {
         "ID": "OneBitesTheDust",
         "Service": "OneBitesTheDust",
         "Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
         "Address": "www.google.com",
         "Port": 80,
         "EnableTagOverride": false,
         "CreateIndex": 72819,
         "ModifyIndex": 72819
     },
     "anotherOneBitesTheDust": {
         "ID": "anotherOneBitesTheDust",
         "Service": "anotherOneBitesTheDust",
         "Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
         "Address": "www.google.com",
         "Port": 80,
         "EnableTagOverride": false,
         "CreateIndex": 72465,
         "ModifyIndex": 72465
     },
     "newService": {
         "ID": "newService",
         "Service": "newService",
         "Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
         "Address": "www.google.com",
         "Port": 80,
         "EnableTagOverride": false,
         "CreateIndex": 72389,
         "ModifyIndex": 72389
     }
  }
}

nock("http://10.244.68.3:8500")
    .get('/v1/catalog/node/services')
    .reply(200, consulResponse);
 });

it("returns a status code of 200 when the services domain is queried", function(done) {
    queryConsul(function(err, res){
      console.log(res);
      expect(res.statusCode).to.equal(200, done);
    });
  });
});

1 个答案:

答案 0 :(得分:0)

所以经过多次挖掘,我们发现了它。我们完全改变了我们的代码并使用了这个非常棒的npm(节点包管理器)

请求承诺宝石......(确保添加" json:true"到选项

var express = require('express');
var router = express.Router();
var request = require('request-promise')


router.get('/', function(req, res, next) {
  res.render('index')
});

router.get('/data', function(req, res, next){
  var options = {
    uri: 'http://10.244.68.3:8500/v1/catalog/node/services',
    json: true
};
  request(options).then(function(result){
    res.send(result)
  }).catch(function(err){
    res.send(err)
  })
 })


module.exports = router;

以下是我们如何创建我们的测试以达到这条路线(" / data"),创建模拟并回馈我们期望的内容

var expect = require("chai").expect;
var nock = require("nock");
var app = require('../../../app');
var request = require("supertest")

var consulResponse

describe("Consul API Queries", () => {
  beforeEach(() => {
    consulResponse =
    {
     "Node": {
         "Node": "Services",
         "Address": "some_address",
         "TaggedAddresses": null,
         "CreateIndex": 72389,
         "ModifyIndex": 72819
     },
     "Services": {
         "OneBitesTheDust": {
             "ID": "OneBitesTheDust",
             "Service": "OneBitesTheDust",
             "Tags": ["{\"Type\" : \"service type\", \"Description\":      \"ddfadf\"}"],
             "Address": "www.google.com",
             "Port": 80,
             "EnableTagOverride": false,
             "CreateIndex": 72819,
             "ModifyIndex": 72819
         },
         "anotherOneBitesTheDust": {
             "ID": "anotherOneBitesTheDust",
             "Service": "anotherOneBitesTheDust",
             "Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
             "Address": "www.google.com",
             "Port": 80,
             "EnableTagOverride": false,
             "CreateIndex": 72465,
             "ModifyIndex": 72465
         },
         "newService": {
             "ID": "newService",
             "Service": "newService",
             "Tags": ["{\"Type\" : \"service type\", \"Description\": \"ddfadf\"}"],
             "Address": "www.google.com",
             "Port": 80,
             "EnableTagOverride": false,
             "CreateIndex": 72389,
             "ModifyIndex": 72389
         }
      }
    }

    nock("http://10.244.68.3:8500")
        .get('/v1/catalog/node/services')
        .reply(200, consulResponse);
  });

  it("returns a status code of 200 when the services domain is    queried", () => {
    var scope = nock("http://10.244.68.3:8500")
        .get('/v1/catalog/node/services')
        .reply(200, "no way are we getting this to work");
    expect(scope.interceptors[0].statusCode).to.equal(200);
  });


  it("gets the data from consul", (done) => {
    request(app).get('/data')
        .expect(consulResponse, done)

  })

});

问题在于请求并未按原样交付承诺,因此您需要使用request-promise gem来实现此目的。一旦你开始这样做,你应该好好去!