title: Node JS - How does one stub a soap function call for unit testing
body: <p>How can I effectively mock out responses to remote system calls while writing unit tests. The method in question is this one:</p>
  
  <pre><code>client.SomeFunction(input, function(err, result)) {...}
  </code></pre>
  
  <p>The code that contains it is here:</p>
  
  <pre><code>'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 &gt; 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;
  };
  </code></pre>
  
  <p>This code is eventually going to be run as an AWS Lambda, which is why the function needs to be named in that fashion.</p>