Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior when using AlbaHost.For #130

Open
firedog opened this issue Dec 5, 2022 · 1 comment
Open

Inconsistent behavior when using AlbaHost.For #130

firedog opened this issue Dec 5, 2022 · 1 comment

Comments

@firedog
Copy link

firedog commented Dec 5, 2022

.NET 6, Alba 7.2.1

Using the following test...

public class Tests
{
    [Fact]
    public async Task Response_Is_Hello_World()
    {
        await using var host = await AlbaHost.For<Program>();

        await host.Scenario(s =>
        {
            s.Get.Url("/");
            s.ContentShouldBe("Hello, World!");
        });
    }
}

..when SUT is...

public class Program
{
    public static void Main(string[] args)
    {
        throw new ApplicationException("Main was called!");
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.Map("/", () => "Hello, World!");
        });
    }
}

...leads to a successful test! (i.e. Main was never called)

Renaming the CreateHostBuilder method to CreateHostBuilder2 and thus using SUT as ...

public class Program
{
    public static void Main(string[] args)
    {
        throw new ApplicationException("Main was called!");
        CreateHostBuilder2(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder2(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.Map("/", () => "Hello, World!");
        });
    }
}

...leads to a failed test as the exception is thrown. (i.e. Main was called)

I guess this has to do with the logic in EnsureServer() (as described by Andrew Lock in https://andrewlock.net/exploring-dotnet-6-part-6-supporting-integration-tests-with-webapplicationfactory-in-dotnet-6/) that looks for older ways of doing things and uses the ASP.NET Core 3.x/5 convention when it finds a method named CreateHostBuilder.

Don't know if there's something to do about this or if just should be documented somewhere?

@Hawxy
Copy link
Collaborator

Hawxy commented Dec 6, 2022

This aligns with the expected behavior of the internal resolver, it'll search for variations of CreateHostBuilder, and if it's not found, kick off the entry-point to check if it's a WebApplicationBuilder project. I expect most people using .NET 6 and newer will be using WebApplicationBuilder, but I'll add a note that the application needs to conform with default template conventions if using CreateHostBuilder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants