我有一个具有以下结构的XUnit测试项目:
_environment.WebRootPath
始终为null,并且在测试项目中有一些依赖该值的代码。我知道我可以在TestStartup中手动设置该值。我想知道是否有比硬编码路径(C:\...\MyApp.Rest\wwwroot
)更好的方法来设置值?
public TestStartup(IHostingEnvironment environment, IServiceProvider serviceProvider) {
_environment = environment;
_environment.WebRootPath = "ugly_hardcoded_path_to_wwwroot"
var builder = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile($"appsettings.{_environment.EnvironmentName}.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
答案 0 :(得分:0)
我在集成测试中遇到了相同的问题。我在TestServer
Microsoft.AspNetCore.TestHost
类
这是我配置WebrootPath的方式:
_server = new TestServer(new WebHostBuilder()
.UseWebRoot(@"The path i want to use")
.UseStartup<Startup>());
_client = _server.CreateClient();