WebRootPath和TestServer [英] WebRootPath and TestServer
问题描述
我有一个具有以下结构的XUnit测试项目:
I have a XUnit test project with the following structure:
- \ MyApp.Rest
- \ wwwroot
- 对"MyApp.Rest"的项目引用
_environment.WebRootPath
始终为null,并且在测试项目中有一些依赖于该值的代码.我知道我可以在TestStartup中手动设置该值.我想知道是否有比硬编码路径(C:\ ... \ MyApp.Rest \ wwwroot
)更好的方法来设置值?_environment.WebRootPath
in my integration tests is always null and I have some code in the tested project that depends on that value. I know I can set the value manually in the TestStartup. I am wondering if there is a better way to set the value other than hard-coding the path (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(); }
推荐答案
我在集成测试中遇到了相同的问题.我在
Microsoft.AspNetCore.TestHost
I came accross the same issue in my integration test. I use
TestServer
class inMicrosoft.AspNetCore.TestHost
这是我配置WebrootPath的方式:
Here is how i configure the WebrootPath :
_server = new TestServer(new WebHostBuilder() .UseWebRoot(@"The path i want to use") .UseStartup<Startup>()); _client = _server.CreateClient();
这篇关于WebRootPath和TestServer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!