【发布时间】: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() WebApplicationFactory
1.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
您有什么正确配置的建议吗?
【问题讨论】: