Unable to unit test simple endpoint with Carter in C Sharp

324 Views Asked by At

Im trying to unit test my REST web api coded in C Sharp Carter framework using ASP.NET TestHost Version 2.1 This is my endpoint :

namespace Api
{
    public class HealthModule : CarterModule
    {
        public HealthModule()
        {
            Get("/health", async (req, res, routeData) => await res.WriteAsync("I'm healthy!"));
        }
    }
}

And this is my test :

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);
        }
    }
}

Note : My app is a NetCore 2.1 app.

Error :

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.

What can be causing the above error?

0

There are 0 best solutions below