【问题标题】:Testing Express route where that route has a callback测试该路由有回调的 Express 路由
【发布时间】:2017-01-25 05:54:11
【问题描述】:

我想模拟一个调用 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;

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

这是我的测试,我尝试使用 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);
    });
  });
});

【问题讨论】:

    标签: express callback nock


    【解决方案1】:

    所以经过更多的挖掘,我们想通了。我们完全改变了我们的代码并使用了这个很棒的 npm(节点包管理器)

    request-promise gem...(确保在选项中添加“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 来实现这一点。一旦你得到这个,你应该很高兴!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-13
      • 2014-06-20
      • 2020-11-30
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多