2016-09-16 8 views
0

私は、curlリクエストを、apiを呼び出すエクスプレスルートに模擬したいと思っています。私はこれを行う方法について多くの文書を見つけましたが、私のコードにコールバックがあるので問題が発生しています。私は私の現在のテストを実行するとそのルートがコールバックを持っているExpressルートをテストする

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) 

    }) 

}); 

問題はそうあなたが要求を必要としているように、その要求は約束を渡していませんでした期待するものを私たちに恩返しするために我々のテストを作成しました - これを得るために宝石を賞賛してください。あなたがこれを行けば、あなたは行くのが良いはずです!

関連する問題