title: How to mock SOAP client that was auto-generated by Visual Studio?
body: <p>I have an auto-generated SOAP client. It was generated by visual studio wizard (add connected services -> Microsoft WCF Web Service Reference Provider). I would like to mock that client, so when a method is called, a predefined result is going to be returned in a format of the SOAP response. Unfortunately, I cannot get it to work - my result is either null (instead of defined in .Returns) or I get an exception. </p>
  
  <p>I am trying to apply clean architecture in my design. So this SOAP client landed in my infrastructure layer, where I have created a repository for it. This repository creates DTOs, so they can be dispatched to my persistence. The repository receives the SOAP client through dependency injection. I would also like to have tests for the repository, just to validate that DTOs generation is correct. So, I would like to mock this SOAP service, so I can feed it to the repository and test the returned DTOs.</p>
  
  <p>Auto-generated interface:</p>
  
  <pre><code>    public interface ApplicationSoap
      {
          [System.ServiceModel.OperationContractAttribute(Action = "http://Application/GetAppVersion", ReplyAction = "*")]
          Task&lt;ExtApp.GetAppVersionResponse&gt; GetAppVersionAsync(ExtApp.GetAppVersionRequest request);
      }
  
  </code></pre>
  
  <p>and auto-generated client class:</p>
  
  <pre><code>    [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.1-preview-30514-0828")]
      public partial class ApplicationSoapClient : System.ServiceModel.ClientBase&lt;ExtApp.ApplicationSoap&gt;, ExtApp.ApplicationSoap
      {
  
          static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
  
          [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
          System.Threading.Tasks.Task&lt;ExtApp.GetAppVersionResponse&gt; ExtApp.ApplicationSoap.GetAppVersionAsync(ExtApp.GetAppVersionRequest request)
          {
              return base.Channel.GetAppVersionAsync(request);
          }
  
          public System.Threading.Tasks.Task&lt;ExtApp.GetAppVersionResponse&gt; GetAppVersionAsync(int appVer)
          {
              ExtApp.GetAppVersionRequest inValue = new ExtApp.GetAppVersionRequest();
              inValue.Body = new ExtApp.GetAppVersionRequestBody();
              inValue.Body.appVer = appVer;
              return ((ExtApp.ApplicationSoap)(this)).GetAppVersionAsync(inValue);
          }
      }
  </code></pre>
  
  <p>I want to mock ApplicationSoapClient and method GetApplicationVersionAsync.
  After different attempts I ended up the following in my test class:</p>
  
  <pre><code>private ExtApp.AppVersion[] _response =
      new ExtApp.AppVersion[]
          {
           new ExtApp.AppVersion
               {
                VersionNumber = 1,
                StartDate = DateTime.Parse("2010-01-01"),
                EndDate = DateTime.Parse("2015-12-31")
               },
          };
  
  private Mock&lt;ExtApp.ApplicationSoap&gt; _client = new Mock&lt;ExtApp.ApplicationSoap&gt;();
  
  public TestClass() 
  {
      var body = new ExtApp.GetAppVersionRequestBody(It.IsAny&lt;int&gt;());
      var request = new ExtApp.GetAppVersionRequestRequest(body);
      _client
             .Setup(s =&gt; s.GetAppVersionAsync(request))                
             .Returns(
                 Task.FromResult(
                     new ExtApp.GetAppVersionResponse(
                         new ExtApp.GetAppVersionResponseBody(_response)))
              );
  }
  
  [Fact]
  public void TestAppVersionDownload()
  {
      var request = new ExtApp.GetAppVersionRequest(
                        new ExtApp.GetAppVersionRequestBody(1));
      var result = _clientSoap.Object.GetAppVersionAsync(request)
                        .Result; //returns null instead of defined in Returns section
      Assert.True(result.Body.GetAppVersionResult.Length == 2);
  }
  </code></pre>
  
  <p>It runs, but Result of the call is <code>null</code>. I am expecting to get back the object that would have a non-null <code>Body</code> property with the array inside. I am looking for advice how to make this thing work.</p>