2017-05-30 56 views
0

我正在使用strong-soap模塊從SOAP請求中獲取數據。如何在節點JS中用sinon存根SOAP客戶端請求?

var soap = require('strong-soap').soap; 

soap.createClient(url, options, function (err, client) { 
    var method = client.GetInfoSOAP; 
    method(requestQuery, function (err, info) { 
     // bla bla 
    } 
} 

我正在獲取所需數據。現在 我想編寫單元測試用例來模擬使用sinon存根的SOAP請求,但沒有得到任何成功。任何幫助,將不勝感激。

+0

我的回答有幫助嗎? – oligofren

回答

0

你想要的是控制soap對象的createClient。你可以做到這一點使用分爲兩類技術:

  1. 依賴注入 - 揭露一個二傳手,通過它你可以注入假的模塊,您自己控制測試
  2. 使用鏈接縫 - 勾入進口/ require機制並重寫模塊正在獲取的內容。

興農項目有a nice page通過proxyquire使用環節的接縫,並且我也有詳細的how to do DI on the issue tracker

爲了實現第一個,所有你需要的是做這樣的事情在模塊中:

module.exports._injectSoap = (fake) => soap = fake; 

然後在您的測試代碼:

const fakeSoap = { createClient : sinon.stub().returns(/* ? */) } 
myModule._injectSoap(fakeSoap); 

... 
assert(fakeSoap.createClient.calledOnce); 
assert(...) 
0

嗨,我已經解決了我的問題與以下代碼:

sinon.stub(soap, 'createClient').yields(null, { 
     GetInfoSOAP: function (request, cb) { 
      return cb(null, myDesiredData); 
     } 
    });