2016-07-08 69 views
1

如何在編寫單元測試時有效地模擬對遠程系統調用的響應。有問題的方法是這樣的一個:節點JS - 如何爲存儲單元測試存根肥皂函數調用

client.SomeFunction(input, function(err, result)) {...} 

包含它的代碼是在這裏:

'use strict'; 
var soap    = require('soap'); 
var responseBuilder = require('./responseBuilder'); 
var errorHandler  = require('../src/errorHandler'); 

/** 
* Execute the soap client 
*/ 
exports.executeSoapClient = function (input, headers, wsdlOptions, wsdlPath) { 

    var soapClientPromise = new Promise(function(resolve, reject){ 

     var response = responseBuilder.buildResponse(); 

     //Call SOAP Client 
     soap.createClient(wsdlPath, wsdlOptions, function (err, client) { 
      if (err) { 
       console.error(err, err.stack); 
       reject(errorHandler.createError("Unexpected: Could not communication with remote system.", 500)); 
      } 

      client.addSoapHeader(headers, "", "tkn", "http://siebel.com/webservices"); 
      //This is the function I want to mock out. 
      client.SomeFunction(input, function (err, result) { 

       if (err) { 
        console.error(err); 
        reject(errorHandler.createError("Unexpected: Unexpected: Could not call remote function on remote system.", 500)); 
       } else { 
        if (result.Error_spcCode > 0) { 
         var errorMessage = result.Error_spcMessage + " (" + result.Error_spcCode + ")"; 
         var error = errorHandler.createError("Business rule violation in remote system: " + errorMessage, 401); 
         console.error(error); 
         reject(error); 
        } else { 
         var successMessage = { flexId: result.FLEXId , message: result.Message }; 
         resolve(response.success(successMessage)); 
        } 
       } 
      }); 
     }); 
    }); 
    return soapClientPromise; 
}; 

該代碼最終會爲什麼功能需要來運行作爲AWS LAMBDA,這是以這種方式命名。

回答