首页 > 解决方案 > 无法在 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.

什么可能导致上述错误?

标签: c#unit-testingasp.net-corecarter

解决方案


推荐阅读