.net Core 3.0,DirectoryNotFoundException 与 Xunit 集成测试

.net Core 3.0, DirectoryNotFoundException with Xunit Integration test

我有一个关于在 .net core 3.0 上迁移我的 Xunit 集成测试的新问题。我将项目移动到子文件夹中,现在出现 DirectoryNotFoundException。

测试服务器夹具:

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;
    }
}

错误信息:

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

项目不在:

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

在TestServerFixture.CreateHostBuilder中,Directory.GetCurrentDirectory()返回的值为

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

我试过了:

    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;
    }

但是它不起作用。

我做了一个回购来向你展示这个问题: https://github.com/ranouf/TestingWithDotNetCore3_0

你有什么正确配置的建议吗?

我在这里找到了可用的解决方案:我找到了适合我的 solution/hack,可在此处找到:

就我而言,解决方案应该是:

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);
    }
}

我通过添加以下我的装置解决了这个问题。如果在 CreateHostBuilder() 中添加 UseContentRoot(Directory.GetCurrentDirectory()) 将不起作用。失去了一个小时追鬼。可笑。

    protected override IHost CreateHost(IHostBuilder builder)
    {
        builder.UseContentRoot(Directory.GetCurrentDirectory());    
        return base.CreateHost(builder);
    }

设置程序集名称为我修复了它