存根或模拟ASP.NET Web API HttpClient [英] Stubbing or Mocking ASP.NET Web API HttpClient

查看:92
本文介绍了存根或模拟ASP.NET Web API HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用新的Web API位,并且发现我无法使用普通的HttpMessageRequest,因为我需要向请求中添加客户端证书.结果,我正在使用HttpClient(所以我可以使用WebRequestHandler).这一切都很好,除非它对存根/模拟不友好,至少对于Rhino Mocks而言.

I am using the new Web API bits in a project, and I have found that I cannot use the normal HttpMessageRequest, as I need to add client certificates to the request. As a result, I am using the HttpClient (so I can use WebRequestHandler). This all works well, except that it isn't stub/mock friendly, at least for Rhino Mocks.

我通常会在HttpClient周围创建一个包装服务,但我想尽可能避免这种情况,因为有很多方法需要包装.我希望我缺少有关如何存根HttpClient的任何建议?

I would normally create a wrapper service around HttpClient that I would use instead, but I would like to avoid this if possible, as there are a lot of methods that I would need to wrap. I am hoping that I have missing something—any suggestions on how to stub HttpClient?

推荐答案

我使用Moq,可以存根HttpClient.我认为Rhino Mock也是一样(我自己没有尝试过). 如果只想对HttpClient存根,则下面的代码应该可以工作:

I use Moq and I can stub out the HttpClient. I think this the same for Rhino Mock (I haven’t tried by myself). If you just want to stub the HttpClient the below code should work:

var stubHttpClient = new Mock<HttpClient>();
ValuesController controller = new ValuesController(stubHttpClient.Object);

如果我错了,请纠正我.我猜您在这里指的是在HttpClient中存根成员.

Please correct me if I’m wrong. I guess you are referring to here is that stubbing out members within HttpClient.

大多数流行的隔离/模拟对象框架不允许您对非虚拟成员进行存根/设置 例如,下面的代码引发异常

Most popular isolation/mock object frameworks won’t allow you to stub/setup on non- virtual members For example the below code throws an exception

stubHttpClient.Setup(x => x.BaseAddress).Returns(new Uri("some_uri");

您还提到要避免创建包装器,因为您将包装许多HttpClient成员.不清楚为什么需要包装许多方法,但是您可以轻松地仅包装所需的方法.

You also mentioned that you would like to avoid creating a wrapper because you would wrap lot of HttpClient members. Not clear why you need to wrap lots of methods but you can easily wrap only the methods you need.

例如:

public interface IHttpClientWrapper  {   Uri BaseAddress { get;  }     }

public class HttpClientWrapper : IHttpClientWrapper
{
   readonly HttpClient client;

   public HttpClientWrapper()   {
       client = new HttpClient();
   }

   public Uri BaseAddress   {
       get
       {
           return client.BaseAddress;
       }
   }
}

我认为可能对您有所帮助的其他选项(这里有很多示例,因此我不会编写代码) Microsoft Moles框架 http://research.microsoft.com/en-us/projects/moles/ Microsoft伪造品:(如果使用的是VS2012 Ultimate) http://msdn.microsoft.com/en-us/library/hh549175.aspx

The other options that I think might benefit for you (plenty of examples out there so I won’t write the code) Microsoft Moles Framework http://research.microsoft.com/en-us/projects/moles/ Microsoft Fakes: (if you are using VS2012 Ultimate) http://msdn.microsoft.com/en-us/library/hh549175.aspx

这篇关于存根或模拟ASP.NET Web API HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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