.net Core 3.0, DirectoryNotFoundException with Xunit Integration test
Asked Answered
G

3

5

I have a new issue working on migrating my Xunit integration test on .net core 3.0. I moved the project in a subfolder and now I have a DirectoryNotFoundException.

TestServerFixture:

public class TestServerFixture : WebApplicationFactory<TestStartup>
{
    public HttpClient Client { get; }
    public ITestOutputHelper Output { get; set; }

    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddXunit(Output);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseStartup<TestStartup>()
                    .ConfigureTestServices((services) =>
                    {
                        services
                            .AddControllers()
                            .AddApplicationPart(typeof(Startup).Assembly);
                    });
            });

        return builder;
    }

    public TestServerFixture SetOutPut(ITestOutputHelper output)
    {
        Output = output;
        return this;
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        Output = null;
    }
}

Error message:

Message: System.IO.DirectoryNotFoundException : **C:\Users\Me\source\repos\tests\TestingWithDotNetCore3_0\MyIntegrationTests** Stack Trace: PhysicalFileProvider.ctor(String root, ExclusionFilters filters) PhysicalFileProvider.ctor(String root) HostBuilder.CreateHostingEnvironment() HostBuilder.Build() WebApplicationFactory1.CreateHost(IHostBuilder builder) WebApplicationFactory1.EnsureServer() WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory1.CreateClient() WeatherForecastController_Tests.ctor(TestServerFixture testServerFixture, ITestOutputHelper output) line 25

The project is not in:

*C:\Users\Me\source\repos\tests\TestingWithDotNetCore3_0\MyIntegrationTests*

In TestServerFixture.CreateHostBuilder, the value returned by Directory.GetCurrentDirectory() is

"C:\Users\Me\source\repos\tests\TestingWithDotNetCore3_0\src\tests\AllInOne.Integration.Tests\bin\Debug\netcoreapp3.0"

I tried:

    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
            **.UseContentRoot(Directory.GetCurrentDirectory())**
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddXunit(Output);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseStartup<TestStartup>()
                    **.UseContentRoot(Directory.GetCurrentDirectory())**
                    .ConfigureTestServices((services) =>
                    {
                        services
                            .AddControllers()
                            .AddApplicationPart(typeof(Startup).Assembly);
                    });
            });

        return builder;
    }

But It doesnt work.

I did a repo to show you the issue: https://github.com/ranouf/TestingWithDotNetCore3_0

Do you have any suggestion to configure it correctly?

Gamosepalous answered 9/12, 2019 at 17:14 Comment(0)
G
0

I found a solution available here: I found a solution/hack which works for me, available here: https://mcmap.net/q/480955/-webapplicationfactory-throws-error-that-contentrootpath-does-not-exist-in-asp-net-core-integration-test

In my case, the solution should be:

public class TestServerFixture : WebApplicationFactory<Startup>
{
    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddXunit(Output);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .ConfigureTestServices((services) =>
                    {
                        services
                            .AddControllers()
                            .AddApplicationPart(typeof(Startup).Assembly);
                    });
            });

        return builder;
    }

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.UseStartup<TestStartup>();    
        base.ConfigureWebHost(builder);
    }
}
Gamosepalous answered 9/12, 2019 at 20:54 Comment(0)
C
14

I solved this by adding the following my fixture. It won't work if you add UseContentRoot(Directory.GetCurrentDirectory()) in CreateHostBuilder(). Lost one-hour chasing ghosts. Ridiculous.

    protected override IHost CreateHost(IHostBuilder builder)
    {
        builder.UseContentRoot(Directory.GetCurrentDirectory());    
        return base.CreateHost(builder);
    }
Calculation answered 3/11, 2021 at 13:21 Comment(0)
G
0

I found a solution available here: I found a solution/hack which works for me, available here: https://mcmap.net/q/480955/-webapplicationfactory-throws-error-that-contentrootpath-does-not-exist-in-asp-net-core-integration-test

In my case, the solution should be:

public class TestServerFixture : WebApplicationFactory<Startup>
{
    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddXunit(Output);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .ConfigureTestServices((services) =>
                    {
                        services
                            .AddControllers()
                            .AddApplicationPart(typeof(Startup).Assembly);
                    });
            });

        return builder;
    }

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.UseStartup<TestStartup>();    
        base.ConfigureWebHost(builder);
    }
}
Gamosepalous answered 9/12, 2019 at 20:54 Comment(0)
C
0

Setting the assembly name fixed it for me

Catafalque answered 29/4, 2022 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.