我正在为我的服务类编写单元测试,但是它调用了外部WCF服务,因此需要模拟此调用。这是外部WCF服务调用的包装器接口。
internal interface IKoopServiceRequestHelper
{
Task<ServiceResponseDTO> CreateRequestAsync(ServiceRequestDTO serviceRequestDTO);
Task<TResponse> SendServiceMessagesAsync<TRequest, TResponse>(TRequest request, string messageCode)
where TRequest : RequestDTO
where TResponse : IResponseDTO;
}
为了模拟该接口的真实实现,我编写了一个如下的存根类。
class StubRequestHelper : IKoopServiceRequestHelper
{
public async Task<ServiceResponseDTO> CreateRequestAsync(ServiceRequestDTO serviceRequestDTO)
{
return await Task.FromResult(new ServiceResponseDTO());
}
}
但是我无法编写名为SendServiceMessagesAsync
的第二种方法的模拟实现,该方法具有两个泛型类型参数。
答案 0 :(得分:2)
尝试使用moq
。为了完全满足约束条件,假设如下:
ServiceRequestDTO : RequestDTO
ServiceResponseDTO : IResponseDTO
使用moq
应该很简单
var mock = new Mock<IKoopServiceRequestHelper>();
mock.Setup(m => m.CreateRequestAsync(It.IsAny<ServiceRequestDTO>()))
.Returns(Task.FromResult(new ServiceResponseDTO()));
mock.Setup(m => m.SendServiceMessagesAsync<ServiceRequestDTO, ServiceResponseDTO>(It.IsAny<ServiceRequestDTO>(), It.IsAny<string>()))
.Returns(Task.FromResult(new ServiceResponseDTO()));