单元测试服务电话

时间:2014-12-11 12:06:49

标签: wcf unit-testing proxy

我正在尝试围绕服务调用单元测试代码 - 我想测试

1. the client.retrieveDetails call is made, 
2. the client.retrieveDetails call is made with the correct request and 
3. the result contains an error code and an exception is thrown if it matches a specific one.

我在类中有以下方法,它也包含其他操作。我需要在每个方法调用(MyClient)中实例化代理,所以我不能在类级别拥有它的一个实例。我该如何对这种方法进行单元测试?

    public virtual List<Detail> GetDetails(long code)
    {
        detail_Type[] details;

            var client = new MyClient();
            var context = ContextV2();
            var result = client.retrieveDetails(
                    ref context,
                    IdentifierV2(code),
                    out details);

            _exceptionGenerator.ThrowExceptionIfCodeIncorrect("" + result.resultCode, result.resultMessage);
        }

        List<Detail> response = Mapper.Map<detail_Type[], List<Detail>>(details);

        return response;
    }

1 个答案:

答案 0 :(得分:2)

  
      
  1. 进行了client.retrieveDetails调用
  2.   
  3. 使用正确的请求和
  4. 进行client.retrieveDetails调用   

您可以围绕服务创建包装并使用模拟。如果您使用像moq这样的模拟框架,则检查方法调用非常容易。

  
      
  1. 结果包含错误代码,如果匹配特定的错误代码,则抛出异常。
  2.   

为此,我将创建单独的测试,仅测试服务实现的行为。