title: Moq with Task await
body: <p>Since I have converted my WCF methods to Async, my unit tests have failed, and I can't figure out the correct syntax to get them to work.</p>
  
  <p><strong>Cllient proxy class</strong></p>
  
  <pre><code> public interface IClientProxy
  {
       Task DoSomething(CredentialDataList credentialData, string store);
  }
  </code></pre>
  
  <p><strong>service class</strong></p>
  
  <pre><code>  public class CredentialSync : ICredentialSync
  {
      private ICredentialRepository _repository;
  
      private IClientProxy _client;
  
      public CredentialSync()
      {
          this._repository = new CredentialRepository();
          this._client = new ClientProxy();
      }
  
      public CredentialSync(ICredentialRepository repository, IClientProxy client)
      {
          this._repository = repository;
          this._client = client;
      }
  
     public async Task Synchronise(string payrollNumber)
      {
          try
          {
              if (string.IsNullOrEmpty(payrollNumber))
              {
                  .... some code
                }
              else
              {
                  CredentialDataList credentialData = new CredentialDataList();
                  List&lt;CredentialData&gt; credentialList = new List&lt;CredentialData&gt;();
  
                  // fetch the record from the database
                  List&lt;GetCredentialData_Result&gt; data = this._repository.GetCredentialData(payrollNumber);
                  var pinData = this._repository.GetCredentialPinData(payrollNumber);
  
                  // get the stores for this employee
                  var storeList = data.Where(a =&gt; a.StoreNumber != null)
                      .GroupBy(a =&gt; a.StoreNumber)
                      .Select(x =&gt; new Store { StoreNumber = x.Key.ToString() }).ToArray();
  
                  var credential = this.ExtractCredentialData(data, pinData, payrollNumber);
  
                  credentialList.Add(credential);
                  credentialData.CredentialList = credentialList;
  
                  foreach (var store in storeList)
                  {       
                    //this line causes an Object reference not set to an instance of an object error
                     await  _client.DoSomething(credentialData, store.StoreNumber);
  
                  }
              }
          }
          catch (Exception ex)
          {
              throw new FaultException&lt;Exception&gt;(ex);
          }
      }
  </code></pre>
  
  <p><strong>Test Class</strong></p>
  
  <pre><code> /// &lt;/summary&gt;
  [TestClass]
  public class SynchTest
  {
  
      private Mock&lt;ICredentialRepository&gt; _mockRepository;
      private Mock&lt;IClientProxy&gt; _mockService;
  
      [TestInitialize]
      public void Setup()
      {
         ... some setups for repository which work fine
      }
  
  [TestMethod]      
      public async Task SynchroniseData_WithOneEmployee_CallsReplicateService()
      {
          this._mockService = new Mock&lt;IClientProxy&gt;();
          this._mockService.Setup(x=&gt;x.DoSomething(It.IsAny&lt;CredentialDataList&gt;(), It.IsAny&lt;string&gt;()));
          // arrange
          string payrollNumber = "1";
          CredentialSync service = new CredentialSync(this._mockRepository.Object, this._mockService.Object);
  
          // act
          await service.Synchronise(payrollNumber);
  
          // assert                 
          this._mockService.VerifyAll();
      }
  </code></pre>
  
  <p>The error is when <code>ClientProxy.DoSomething</code> is called:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>The parameters are both fine.</p>
  
  <p>If I convert my <code>ClientProxy.DoSomething</code> method to a synchronous method
  (public void <code>DoSomething(...)</code> )the code works fine, but I do need this to be called asynchronously</p>