0

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?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Matias Barrios
  • 4,674
  • 3
  • 22
  • 49
  • Technically this looks more like an integration test. That aside. did you check to make sure the state dependencies are referenced by the test project? – Nkosi Sep 24 '19 at 20:36
  • Yes. I checked both the dependencias of my "real" project and the test version. It's the same. And the real one is running fine. – Matias Barrios Sep 24 '19 at 20:38
  • 1
    Also avoid mixing async-await and blocking class like `.Result` which can lead to deadlocks. Make the tests async as well. ie `response = await client.GetAsync("/health")` – Nkosi Sep 24 '19 at 20:38
  • @Nkosi I actually copied from Carter repo itself but nice tip – Matias Barrios Sep 24 '19 at 20:39
  • Can you include a link to the docs. Want to check versions – Nkosi Sep 24 '19 at 20:42
  • @Nkosi this is the link to testhost : https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost?view=aspnetcore-2.2 – Matias Barrios Sep 24 '19 at 20:44
  • @Nkosi and here the carter sample tests : https://github.com/ritasker/CarterDemos/tree/master/SampleTests – Matias Barrios Sep 24 '19 at 20:46

0 Answers0