How do I upgrade highly customised TestServer setup from .NET Core 3.0 to 3.1

432 views Asked by At

I have a TestServer that extends the AspNetCore.TestHost.TestServer that I am upgrading as a part of the .NET Core upgrade to 3.1, upgrading the projects works fine.

<TargetFramework>netcoreapp3.0</TargetFramework>

But as soon as I upgrade Microsoft.AspNetCore.TestHost from 3.0.0 to 3.1.x my tests run in an infinite loop. I have tried to convert my TestServer to the new setup like this: https://andrewlock.net/converting-integration-tests-to-net-core-3/ but we have so many extensions and customisations it's difficult to translate. I suspect the issue is in our mocks reading from very large json files.

The TestServer looked like this:

public class MyTestServer : TestServer
{
    private MyTestServer(IWebHostBuilder builder) : base(builder)
    {
        TestHelperSettings = UriTestHelperSettings.Default;
    }

    public UriTestHelperSettings TestHelperSettings { get; }

    public static MyTestServer Create<TStartup>() where TStartup : class
    {
        var builder = new WebHostBuilder()
            .ConfigureAppConfiguration((context, config) =>
            {
                config.AddJsonFile("appsettings.Test.json", false);
            })
            .UseStartup<TStartup>()
            .UseEnvironment("Development");

        var testServer = new MyTestServer(builder)
        {
            ApiExporer = UriTestHelpers.ApiExplorer
        };

        return testServer;
    }

    public IApiDescriptionGroupCollectionProvider ApiExporer { get; private set; }

    public static MyTestServer Create() => Create<TestStartup>();
}

and my attempt, one of many to convert it looks like this:

    public class MyTestServer : TestServer
{
    private MyTestServer(IWebHostBuilder builder) : base(builder)
    {
    }

    public static TestServer Create<TStartup>() where TStartup : class
    {
        var builder = new HostBuilder()
            .ConfigureAppConfiguration((context, config) =>
            {
                config.AddJsonFile("appsettings.Test.json", false);
            })
            .ConfigureWebHost(webHost =>
            {
                webHost.UseTestServer();
                webHost.UseStartup<TestStartup>();
                webHost.UseEnvironment("Development");
            });

        var host = CreateHost(builder);
        TestClient = host.GetTestClient();
        host.GetTestServer().AllowSynchronousIO = true;
        return host.GetTestServer();
    }

    public static IHost HostWeb { get; set; }

    protected static IHost CreateHost(IHostBuilder builder)
    {
        HostWeb = builder.Build();

        HostWeb.Start();
    
        return HostWeb;
    }

    public static UriTestHelperSettings TestHelperSettings => UriTestHelperSettings.Default;

    public static IApiDescriptionGroupCollectionProvider ApiExporer => UriTestHelpers.ApiExplorer;

    public static TestServer Create() => Create<TestStartup>();

    public static HttpClient TestClient { get; set; }
}

I have tried desperately to create a minimum running project, but the number of classes needed escalated and worried me that I was exposing sensitive company data.

0

There are 0 answers