2013-10-23 44 views
0
 public class RefDataProvider : IRefDataProvider 
    {    
     private const string REF_DATA_COUNTRIES = "CountryData"; 

     public IEnumerable<CountryLookupDto> GetCountries() 
     { 
      //if in cache then get cached version 
      if (CacheManager.GetInstance.OCache.Contains(REF_DATA_COUNTRIES)) 
       return CacheManager.GetInstance.GetTypedItem<IEnumerable<CountryLookupDto>>(REF_DATA_COUNTRIES); 

      //not in cache so get from dadtavase 
      using (var service = new CrmServiceClient()) 
      { 
       try 
       { 
        IEnumerable<CountryLookupDto> 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<IEnumerable<CountryLookupDto>>(REF_DATA_COUNTRIES); 
     } 
} 

试图对该方法进行单元测试。遇到wcf客户端调用问题。如何做单元测试一个wcf客户端调用的服务

我想在单元测试中验证CrmServiceClient()调用。有没有办法在单元测试中测试wcf调用。请指教。

[TestFixture] 
    public class TestRefDataProvider 
    { 
     private IReferenceDataProvider _referenceDataProvider; 

     [SetUp] 
     public void SetUp() 
     { 
      _referenceDataProvider = new ReferenceDataProvider(); 
     } 

     [Test] 
     public void Verify_GetCountries() 
     { 
      Assert.IsNotNull(_referenceDataProvider.GetCountries()); 
     } 
    } 

谢谢伊利亚。伊利亚解释后:我想出了这一点:

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<CountryLookupDto> GetCountries() 
    { 
     //if in cache then get cached version 
     if (CacheManager.GetInstance.OCache.Contains(REF_DATA_COUNTRIES)) 
      return CacheManager.GetInstance.GetTypedItem<IEnumerable<CountryLookupDto>>(REF_DATA_COUNTRIES); 
     try 
     { 
      IEnumerable<CountryLookupDto> 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<IEnumerable<CountryLookupDto>>(REF_DATA_COUNTRIES); 
    } 

}

我在这里的问题是,我()之前有service.Close。现在我无法使用它。那安全吗?

回答

0

如果CrmServiceClient是您的WCF服务,那么您应该有一个接口ICrmServiceClient。 因此,您不应在代码中创建CrmServiceClient的新实例。唯一你需要的是ICrmServiceClient(例如,通过构造函数)的依赖

public class RefDataProvider : IRefDataProvider 
{    
    private readonly ICrmServiceClient crmServiceClient; 

    public RefDataProvider(ICrmServiceClient crmServiceClient) 
    { 
     this.crmServiceClient = crmServiceClient; 
    } 

    public IEnumerable<CountryLookupDto> GetCountries() 
    { 
     /* your code */ 
    } 
} 

在这种情况下,可以注入模拟OK ICrmServiceClient容易。

[TestFixture] 
public class TestRefDataProvider 
{ 
    private Mock<ICrmServiceClient> crmServiceClientMock; 
    private IReferenceDataProvider _referenceDataProvider; 

    [SetUp] 
    public void SetUp() 
    { 
     crmServiceClientMock = new Mock<ICrmServiceClient>(); 
     crmServiceClientMock 
      .Setuo(/* your code */) 
      .Returns(/* your code */); 
     _referenceDataProvider = new ReferenceDataProvider(
      crmServiceClientMock.Object 
      ); 
    } 
} 

MOQ框架用于模拟依赖关系。

+0

这真的很有道理。我想知道为crmserviceClient提供接口的最佳方式。 – Tun

+0

对于生成的类CrmServiceClient,应该有一个接口ICrmService或CrmService。 –

+0

谢谢伊利亚。我编辑了我的问题。你能告诉我你在使用service.close和没有使用它(使用Ioc)之间怎么看。 – Tun

相关问题