Aspnetcore.docs: Possible Bug - System.ArgumentException : The content root '' does not exist

Created on 14 Jun 2018  路  5Comments  路  Source: dotnet/AspNetCore.Docs

I am seeing an issue when I try to create an integration test with a custom WebApplicationFactory. I get the error below but what is puzzling is the path is missing one folder in the path and I don't know where this value is coming from. It should be '/Users/richardguthrie/go/src/github.com/Azure-Samples/openhack-devops-team/apis/poi/tests/IntegrationTests'

The code is checked in at: https://github.com/Azure-Samples/openhack-devops-team/tree/unit_test3

Is this a bug or am I missing something when I try to init the webhost that would fix this? My intent is to spin up a server locally with in memory db to run the integration test. This happens when I run Debug Test from within VSCode using C# extension.

[xUnit.net 00:00:26.3698120] IntegrationTests.POIIntegrationTests.Get_EndpointsReturnSuccessAndCorrectContentType(url: "/api/poi/") [FAIL]
[xUnit.net 00:00:26.3715660] System.ArgumentException : The content root '/Users/richardguthrie/go/src/github.com/Azure-Samples/openhack-devops-team/apis/poi/IntegrationTests' does not exist.
[xUnit.net 00:00:26.3716860] Parameter name: contentRootPath
[xUnit.net 00:00:26.3732960] Stack Trace:
[xUnit.net 00:00:26.3754940] at Microsoft.AspNetCore.Hosting.Internal.HostingEnvironmentExtensions.Initialize(IHostingEnvironment hostingEnvironment, String contentRootPath, WebHostOptions options)
[xUnit.net 00:00:26.3756430] at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
[xUnit.net 00:00:26.3757000] at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
[xUnit.net 00:00:26.3758120] at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
[xUnit.net 00:00:26.3758790] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateServer(IWebHostBuilder builder) [xUnit.net 00:00:26.3760500] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.EnsureServer()
[xUnit.net 00:00:26.3761230] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) [xUnit.net 00:00:26.3762400] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateClient

P4 Source - Docs.ms

Most helpful comment

@rguthriemsft FWIW I have the exist same error, and google has lead me to the only other person on the planet who has seen the same error. In my case the Content root is also missing a tests folder.

I've followed the article which guardrex suggested, and in my case adding builder.UseContentRoot(".") fixes the problem. Working code:

    public class ProviderStateApiFactory : WebApplicationFactory<ProviderStateApi>
    {
        protected override IWebHostBuilder CreateWebHostBuilder()
        {
            return WebHost.CreateDefaultBuilder()
                .UseStartup<ProviderStateApi>();
        }

        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.UseContentRoot(".");
            base.ConfigureWebHost(builder);
        }
    }

All 5 comments

Hello @rguthriemsft You might need to set the content root path explicitly with UseSolutionRelativeContentRoot. See: https://docs.microsoft.com/aspnet/core/test/integration-tests#how-the-test-infrastructure-infers-the-app-content-root-path

Beyond that, the docs repo primarily handles docs issues ... we aren't really staffed to handle support requests. You'd probably need to open this on the Home repo (https://github.com/aspnet/Home/issues) for help or try Stack Overflow. Certainly if you run into a docs problem that would have helped, please let us know so we can update the content.

@rguthriemsft FWIW I have the exist same error, and google has lead me to the only other person on the planet who has seen the same error. In my case the Content root is also missing a tests folder.

I've followed the article which guardrex suggested, and in my case adding builder.UseContentRoot(".") fixes the problem. Working code:

    public class ProviderStateApiFactory : WebApplicationFactory<ProviderStateApi>
    {
        protected override IWebHostBuilder CreateWebHostBuilder()
        {
            return WebHost.CreateDefaultBuilder()
                .UseStartup<ProviderStateApi>();
        }

        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.UseContentRoot(".");
            base.ConfigureWebHost(builder);
        }
    }

Found a simpler workaround.

public class BlogAppFactory : WebApplicationFactory<Startup>
{
    protected override IWebHostBuilder CreateWebHostBuilder()
    {
        return WebHost.CreateDefaultBuilder()
            .UseStartup<TestStartup>();
    }
}

I don't think this works anymore for .net core v3 and up

[edit]
With this I'm referring to @mattfrear his workaround

@maurei @mattfrear workaround works for me with Net Core 3.1.

Was this page helpful?
0 / 5 - 0 ratings