How to pass in a mocked HttpClient in a .NET test?(如何在 .NET 测试中传入模拟的 HttpClient?)
问题描述
我有一个服务,它使用 Microsoft.Net.Http
来检索一些 Json
数据.太好了!
I have a service which uses Microsoft.Net.Http
to retrieve some Json
data. Great!
当然,我不希望我的单元测试到达实际的服务器(否则,这是一个集成测试).
Of course, I don't want my unit test hitting the actual server (otherwise, that's an integration test).
这是我的服务 ctor(使用依赖注入...)
Here's my service ctor (which uses dependency injection...)
public Foo(string name, HttpClient httpClient = null)
{
...
}
我不知道如何用......说.. Moq
或 FakeItEasy
来模拟它.
I'm not sure how I can mock this with ... say .. Moq
or FakeItEasy
.
我想确保当我的服务调用 GetAsync
或 PostAsync
.. 然后我可以伪造这些调用.
I want to make sure that when my service calls GetAsync
or PostAsync
.. then i can fake those calls.
有什么建议吗?
我-希望-我不需要制作自己的 Wrapper .. 因为那是废话 :( 微软不可能对此进行疏忽,对吧?
I'm -hoping- i don't need to make my own Wrapper .. cause that's crap :( Microsoft can't have made an oversight with this, right?
(是的,制作包装很容易......我以前做过......但这就是重点!)
(yes, it's easy to make wrappers .. i've done them before ... but it's the point!)
推荐答案
你可以用一个假的 HttpMessageHandler 替换核心.看起来像这样的东西......
You can replace the core HttpMessageHandler with a fake one. Something that looks like this...
public class FakeResponseHandler : DelegatingHandler
{
private readonly Dictionary<Uri, HttpResponseMessage> _FakeResponses = new Dictionary<Uri, HttpResponseMessage>();
public void AddFakeResponse(Uri uri, HttpResponseMessage responseMessage)
{
_FakeResponses.Add(uri, responseMessage);
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
if (_FakeResponses.ContainsKey(request.RequestUri))
{
return Task.FromResult(_FakeResponses[request.RequestUri]);
}
else
{
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound) { RequestMessage = request });
}
}
}
然后您可以创建一个将使用假处理程序的客户端.
and then you can create a client that will use the fake handler.
var fakeResponseHandler = new FakeResponseHandler();
fakeResponseHandler.AddFakeResponse(new Uri("http://example.org/test"), new HttpResponseMessage(HttpStatusCode.OK));
var httpClient = new HttpClient(fakeResponseHandler);
var response1 = await httpClient.GetAsync("http://example.org/notthere");
var response2 = await httpClient.GetAsync("http://example.org/test");
Assert.Equal(response1.StatusCode,HttpStatusCode.NotFound);
Assert.Equal(response2.StatusCode, HttpStatusCode.OK);
这篇关于如何在 .NET 测试中传入模拟的 HttpClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 .NET 测试中传入模拟的 HttpClient?


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04