2016-09-16 90 views
0

我想嘲笑一個捲曲請求到一個調用api的快速路由。我發現了很多關於如何做到這一點的文檔,但我一直有問題,因爲我的代碼中有一個回調。測試快速路由,該路由有回調

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的

超時這裏是我的測試中,我試圖用箭扣作爲存根服務,任何幫助將不勝感激! !

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); 
    }); 
    }); 
}); 

回答

0

所以經過更多的挖掘,我們計算出來了。我們完全改變了我們的代碼,並使用該真棒NPM(節點包管理器)

請求,承諾寶石...(請務必添加「JSON:真」的選項

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; 

下面是如何我們創建了測試,以達到此路線(「/數據」),創建一個模擬,並給我們帶回了我們所期望的

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) 

    }) 

}); 

的問題是,請求不用手承諾爲的就是讓你需要的要求 - 鼓勵寶石去實現這個目標,一旦你得到了這個機會,你應該很好去!