WebApplicationFactory: How to use .NET 6.0 WebApplication.CreateBuilder instead of older Host.CreateDefaultBuilder()?
Asked Answered
U

0

1

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://mcmap.net/q/590861/-how-to-use-startup-class-inside-program-file-on-net-6.

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?

Unreflective answered 2/11, 2023 at 10:18 Comment(9)
Just use the standard integration testing approach for minimal hosting model (do not confuse it with Minimal APIs - it is another concept) - for example as described in this answer. Startup in the new (minimal) hosting model is just an POCO class with no special meaning.Ascribe
Does this answer your question? Integration test and hosting ASP.NET Core 6.0 without Startup classAscribe
Thanks @GuruStron, I did come across that link, but what I'm trying to understand is how can I create tests which call different WebApplicationFactory instances which each take a different TEntryPoint using this approach. I cannot create multiple "Program" classes due to the following error: "CS8802: Only one compilation unit can have top-level statements." This used to be easy before; all you had to do is use a different startup class as an entry point.Unreflective
Why do you have multiple Startup classes at all (that is a bit pointless in the minimal hosting model)? How Program will select correct startup class to use? Use the same mechanism to make the WebApplicationFactory's (I assume something with configuration).Ascribe
Also note that there is no need to migrate to the "new" minimal hosting model. You can keep the "old" generic one.Ascribe
I figured I'd use multiple preset Startup classes instead of using the .WithWebHostBuilder method calls. I've now started using that method instead, and that solved it, so thanks! Now I have a new problem though, I am getting a "BadRequest" response on any request I send to a test controller I have in the test project. I am calling CreateClient() and then sending a request. What am I missing now?Unreflective
Without full repro it is hard to tell. Can you post a minimal reproducible example somewhere (like github)?Ascribe
I don't understand why you try to accomplish. I suggest you add in the question a test that use TestWebApiFactory as desired.Strongroom
I found out that the "BadRequest" response is happening whenever I add the [ApiController] attribute to my controller. I am trying really hard to reproduce this behaviour in a new simple test project, but to no avail. There is something in my production code which is causing this, but can't for the life of me figure it out. (Note: This used to work without any issue before I switched to the minimal hosting model)Unreflective

© 2022 - 2024 — McMap. All rights reserved.