I have successfully converted my web applications to start using the new WebApplication.CreateBuilder(args) method, but I am not managing to replicate this success in my tests using a custom (derived) WebApplicationFactory.

I decided to keep a loose concept of a Startup class in my codebase (due to inheritance and what not), but I now call it manually as per https://stackoverflow.com/a/74506909/5044734.

My code used to work like this:

public class TestWebApiFactory<TStartup> : WebApplicationFactory<TStartup>
    where TStartup : class
{
    public TestWebApiFactory()
    {
    }

    protected override void ConfigureClient(HttpClient client)
    {
        base.ConfigureClient(client);
    }

    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
                        .ConfigureAppConfiguration((context, config) =>
                        {
                            config.AddJsonFile("appsettings.json");                                    
                        })
                      .ConfigureWebHostDefaults(x =>
                      {
                          x.UseStartup<TStartup>().UseTestServer();
                      });
        return builder;
    }       
}

How do I achieve the same thing using the WebApplication model instead?

0

There are 0 best solutions below