如何用shim编写wcf调用方法的单元测试代码

本文关键字:方法 单元测试 代码 调用 wcf 何用 shim 编写 | 更新日期: 2023-09-27 18:05:17

我有一个在xamarin中调用wcf的方法,并希望使用shim fake

创建该方法的测试代码
    public Dictionary<int, string> GetProductCategory()
    {
        BasicHttpBinding binding = AceWCFBinding.CreateBasicHttp();
        using ( AceWCFClientWrapper<AceVqbzServiceClient> objAceWCFClientWrapper = new AceWCFClientWrapper<AceVqbzServiceClient>( binding, AceWCFBinding.EndPoint ) )
        {
            IAceVqbzService aceVqbzTypeService = objAceWCFClientWrapper._proxy as IAceVqbzService;
            var _task = Task<Dictionary<int, string>>.Factory.FromAsync( aceVqbzTypeService.BeginGetProductCategory, aceVqbzTypeService.EndGetProductCategory, TaskCreationOptions.None );
            Dictionary<int, string> _res = _task.Result;
            return _res;
        }
    } 

请告诉我如何管理这个方法的测试代码

如何用shim编写wcf调用方法的单元测试代码

这取决于你想测试什么:

  1. GetProductCategory()方法没有任何逻辑。它只是从webservice获取数据。

  2. 如果你想伪造结果字典,那么只需将方法标记为虚拟并在自定义类中重写GetProductCategory(或使用Moq, NSubstitute..)并实现它以返回你的自定义字典。[此解决方案将依赖项拖放到wcf客户端]

  3. 如果你不想把依赖关系拖到WCF客户端,那么用GetProductCategory方法提取接口。然后用它来装饰现在的班级。出于测试目的,您现在可以基于该接口创建模拟、存根等。