title: How to do unit test a service which has wcf client called
body: <pre><code>     public class RefDataProvider : IRefDataProvider
      {            
          private const string REF_DATA_COUNTRIES = "CountryData";
  
          public IEnumerable&lt;CountryLookupDto&gt; GetCountries()
          {
              //if in cache then get cached version
              if (CacheManager.GetInstance.OCache.Contains(REF_DATA_COUNTRIES))
                  return CacheManager.GetInstance.GetTypedItem&lt;IEnumerable&lt;CountryLookupDto&gt;&gt;(REF_DATA_COUNTRIES);
  
              //not in cache so get from dadtavase
              using (var service = new CrmServiceClient())
              {
                  try
                  {
                      IEnumerable&lt;CountryLookupDto&gt; countriesDto = service.LookupCountries("*");
                      bool addedToCache = CacheManager.GetInstance.AddItemWithExpiration(REF_DATA_COUNTRIES, countriesDto,
                                                                                         12);
                      if (!addedToCache) throw new Exception("Cannot add ref data to cache");
                  }
                  catch (Exception ex)
                  {
                      LoggingManager.GetInstance.Log("Error", ex, LoggingManager.LogLevel.Error);
                      throw;
                  }
                  finally
                  {
                      service.Close();
                  }
              }
  
              return CacheManager.GetInstance.GetTypedItem&lt;IEnumerable&lt;CountryLookupDto&gt;&gt;(REF_DATA_COUNTRIES);
          }
  }
  </code></pre>
  
  <p>Trying to do unit test onto the method.  Having problem with wcf client call.</p>
  
  <p>I am trying to verify CrmServiceClient() calls in unit test. Is there any way to test wcf calls in unit test. Please advise.</p>
  
  <pre><code>[TestFixture]
      public class TestRefDataProvider
      {
          private IReferenceDataProvider _referenceDataProvider;
  
          [SetUp]
          public void SetUp()
          {
              _referenceDataProvider = new ReferenceDataProvider();
          }
  
          [Test]
          public void Verify_GetCountries()
          {
              Assert.IsNotNull(_referenceDataProvider.GetCountries());
          }
      }
  </code></pre>
  
  <p>Thanks Ilya. After Ilya explains: I came out with this:</p>
  
  <pre><code>public class ReferenceDataProvider : IReferenceDataProvider
  {
      private const string REF_DATA_TITLE = "TitleData";
      private const string REF_DATA_COUNTRIES = "CountryData";
  
      private readonly ICrmService _crmService;
      public ReferenceDataProvider(ICrmService crmService)
      {
          _crmService = crmService;
      }
  
  
      public IEnumerable&lt;CountryLookupDto&gt; GetCountries()
      {
          //if in cache then get cached version
          if (CacheManager.GetInstance.OCache.Contains(REF_DATA_COUNTRIES))
              return CacheManager.GetInstance.GetTypedItem&lt;IEnumerable&lt;CountryLookupDto&gt;&gt;(REF_DATA_COUNTRIES);
          try
          {
              IEnumerable&lt;CountryLookupDto&gt; countriesDto = _crmService.LookupCountries("*");
  
              bool addedToCache = CacheManager.GetInstance.AddItemWithExpiration(REF_DATA_COUNTRIES, countriesDto,
                  12);
              if (!addedToCache) throw new Exception("Cannot add ref data to cache");
          }
          catch (Exception ex)
          {
              LoggingManager.GetInstance.Log("Error", ex, LoggingManager.LogLevel.Error);
              throw;
          }
  
          return CacheManager.GetInstance.GetTypedItem&lt;IEnumerable&lt;CountryLookupDto&gt;&gt;(REF_DATA_COUNTRIES);
      }
  </code></pre>
  
  <p>}</p>
  
  <p>My question here is I had service.Close() before. Now I can't use it. Is that safe ?</p>