我在将我的Xunit集成测试迁移到.NET Core 3.0时遇到了新问题。我将项目移动到子文件夹中,现在出现了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(String root, ExclusionFilters filters) PhysicalFileProvider.ctor(String root) HostBuilder.CreateHostingEnvironment() HostBuilder.Build() WebApplicationFactory
1.CreateHost(IHostBuilder builder) WebApplicationFactory
1.EnsureServer() WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory
1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory
1.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;
}
但它不起作用。
我创建了一个repo来展示这个问题:https://github.com/ranouf/TestingWithDotNetCore3_0
您有没有任何建议来正确配置它?