【问题标题】:.net Core 3.0, DirectoryNotFoundException with Xunit Integration test.net Core 3.0,DirectoryNotFoundException 与 Xunit 集成测试
【发布时间】:2020-04-02 20:06:17
【问题描述】:

我在 .net core 3.0 上迁移我的 Xunit 集成测试时遇到了一个新问题。我将项目移到了一个子文件夹中,现在我有一个 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;
    }
}

错误信息:

消息: System.IO.DirectoryNotFoundException:**C:\Users\Me\source\repos\tests\TestingWithDotNetCore3_0\MyIntegrationTests** 堆栈跟踪: PhysicalFileProvider.ctor(字符串根,ExclusionFilters 过滤器) PhysicalFileProvider.ctor(字符串根) 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) 第 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;
    }

但它不起作用。

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

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

【问题讨论】:

    标签: c# .net-core xunit


    【解决方案1】:

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

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

    【讨论】:

    • 这对我有用。谢谢。
    【解决方案2】:

    我在这里找到了一个可用的解决方案:我找到了一个适合我的解决方案/hack,可以在这里找到:https://stackoverflow.com/a/57476187/6123422

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-27
      • 2018-05-27
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2019-08-10
      相关资源
      最近更新 更多