如何对具有wcf客户端的服务进行单元测试 [英] How to do unit test a service which has wcf client called

查看:99
本文介绍了如何对具有wcf客户端的服务进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

     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客户端调用有问题.

Trying to do unit test onto the method. Having problem with wcf client call.

我正在尝试在单元测试中验证CrmServiceClient()调用.有什么方法可以在单元测试中测试wcf调用.请告知.

I am trying to verify CrmServiceClient() calls in unit test. Is there any way to test wcf calls in unit test. Please advise.

[TestFixture]
    public class TestRefDataProvider
    {
        private IReferenceDataProvider _referenceDataProvider;

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

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

感谢伊利亚.在Ilya解释之后:我想到了这一点:

Thanks Ilya. After Ilya explains: I came out with this:

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().现在我不能使用它.这样安全吗?

My question here is I had service.Close() before. Now I can't use it. Is that safe ?

推荐答案

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

If CrmServiceClient is your WCF service so you should have an interface ICrmServiceClient. Therefore you should not create new instance of CrmServiceClient in your code. The only thing your need is a dependency on ICrmServiceClient (e.g. via constructor)

public class RefDataProvider : IRefDataProvider
{            
    private readonly ICrmServiceClient crmServiceClient;

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

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

在这种情况下,可以很容易地将模拟注入.

In this case it is possible to inject mock ok ICrmServiceClient easily.

[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 框架用于模拟依赖关系.

MOQ framework is used in order to mock dependencies.

这篇关于如何对具有wcf客户端的服务进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆