【问题标题】:How to do unit test a service which has wcf client called如何对调用了 wcf 客户端的服务进行单元测试
【发布时间】:2013-10-23 15:51:20
【问题描述】:
     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 客户端调用有问题。

我正在尝试在单元测试中验证 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());
        }
    }

谢谢伊利亚。在 Ilya 解释后:我想出了这个:

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() 。现在我不能使用它了。那安全吗?

【问题讨论】:

    标签: c# wcf unit-testing asp.net-mvc-4 tdd


    【解决方案1】:

    如果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 */
        }
    }
    

    在这种情况下,可以轻松注入 mock 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 框架用于模拟依赖项。

    【讨论】:

    • 这真的很有意义。我想知道为 crmserviceClient 提供接口的最佳方式是什么。
    • 对于生成的类CrmServiceClient应该有一个接口ICrmService或者CrmService。
    • 感谢伊利亚。我已经编辑了我的问题。你能告诉我你认为有 service.close 和没有它(使用 Ioc)之间是什么吗?
    • 我不调用 service.Close() 并依赖 IoC。好问题,现在我想看看 IoC 如何与这些代理一起工作。
    • 嗨,你们有没有想出如何处理close() 方法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 2011-04-25
    • 2023-03-16
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多