c# - 无法在 C Sharp 中使用 Carter 对简单端点进行单元测试
问题描述
我正在尝试使用ASP.NET TestHost 版本 2.1对我在 C Sharp Carter 框架中编码的 REST web api 进行单元测试 这是我的端点:
namespace Api
{
public class HealthModule : CarterModule
{
public HealthModule()
{
Get("/health", async (req, res, routeData) => await res.WriteAsync("I'm healthy!"));
}
}
}
这是我的测试:
namespace Api.Tests
{
public class HealthModuleTests
{
private HttpResponseMessage response;
public HealthModuleTests()
{
var client = new TestServer(new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddCarter();
})
.Configure(app => { app.UseCarter(); })
).CreateClient();
response = client.GetAsync("/health").GetAwaiter().GetResult();
}
[Fact]
public void HealthRequestOK()
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("I'm healthy!", response.ReasonPhrase);
}
}
}
注意:我的应用是 NetCore 2.1 应用。
错误 :
Result Message:
System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.Net.Http.Headers, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
什么可能导致上述错误?
解决方案
推荐阅读
- graph - YOLO图形解释
- active-directory - WSO2 身份服务器使用 Active Directory
- angular - Angular 8 CSS 不会在刷新、生产构建时加载
- php - 如何在php路由中使用外部函数
- httprequest - 如何使用请求级客户端在akka http httpRequest中设置无缓存标头
- c# - 如何在使用 xmlserializer c# 反序列化时更改 xml 属性?
- reactjs - 无法从 reactjs 中的 axios 向 id=1 文章的 django REST API 后端发出 GET 请求
- c# - 确定关键字周围的单词的有效方法是什么?
- python - 在python3中出现错误“只能将str(而不是“dict”)连接到str”
- asp.net - 概念/迁移- ASP.Net Webforms 到.Net Core Razor / 路由| 映射