title: How to mock WCF client using Moq?
body: <p>In my project I am using: SL5+ MVVM+ Prism + WCF + Rx + Moq + Silverlight Unit Testing Framework.</p>
  
  <p>I am new to unit-testing and have recently started into DI, Patterns (MVVM) etc. Hence the following code has a lot of scope for improvement (please fell free to reject the whole approach I am taking if you think so).</p>
  
  <p>To access my WCF services, I have created a factory class like below (again, it can be flawed, but please have a look):</p>
  
  <pre><code>namespace SomeSolution
  {
      public class ServiceClientFactory:IServiceClientFactory
      {
          public CourseServiceClient GetCourseServiceClient()
          {
              var client = new CourseServiceClient();
              client.ChannelFactory.Faulted += (s, e) =&gt; client.Abort();
              if(client.State== CommunicationState.Closed){client.InnerChannel.Open();}
              return client;
          }
  
          public ConfigServiceClient GetConfigServiceClient()
          {
              var client = new ConfigServiceClient();
              client.ChannelFactory.Faulted += (s, e) =&gt; client.Abort();
              if (client.State == CommunicationState.Closed) { client.InnerChannel.Open(); }
              return client;
          }
  
          public ContactServiceClient GetContactServiceClient()
          {
              var client = new ContactServiceClient();
              client.ChannelFactory.Faulted += (s, e) =&gt; client.Abort();
              if (client.State == CommunicationState.Closed) { client.InnerChannel.Open(); }
              return client;
          }
      }
  }
  </code></pre>
  
  <p>It implements a simple interface as below:</p>
  
  <pre><code>public interface IServiceClientFactory
  {
      CourseServiceClient GetCourseServiceClient();
      ConfigServiceClient GetConfigServiceClient();
      ContactServiceClient GetContactServiceClient();
  }
  </code></pre>
  
  <p>In my VMs I am doing DI of the above class and using Rx to call WCF as below:</p>
  
  <pre><code>var client = _serviceClientFactory.GetContactServiceClient();
  try
  {
  
      IObservable&lt;IEvent&lt;GetContactByIdCompletedEventArgs&gt;&gt; observable =
          Observable.FromEvent&lt;GetContactByIdCompletedEventArgs&gt;(client, "GetContactByIdCompleted").Take(1);
  
      observable.Subscribe(
          e =&gt;
              {
                  if (e.EventArgs.Error == null)
                  {                                    
                      //some code here that needs to be unit-tested
  
                  }
              },
          ex =&gt;
          {
              _errorLogger.ProcessError(GetType().Name, MethodBase.GetCurrentMethod().Name, ErrorSource.Observable, "", -1, ex);
          }
          );
      client.GetContactByIdAsync(contactid, UserInformation.SecurityToken);
  }
  catch (Exception ex)
  {
      _errorLogger.ProcessError(GetType().Name, MethodBase.GetCurrentMethod().Name, ErrorSource.Code, "", -1, ex);
  }
  </code></pre>
  
  <p>Now I want to build unit tests (yes, its not TDD). But I don't understand where to start. With Moq I can't mock the BlahServiceClient. Also no svcutil generated interface can help because async methods are not part of the auto-generated IBlahService interface. I may prefer to extend (through partial classes etc) any of the auto generated classes, but I would hate to opt for manually building all the code that svcutil can generate (frankly considering time and budget).</p>
  
  <p>Can someone please help? Any pointer in the right direction would help me a lot.</p>