简体   繁体   English

WebRootPath和TestServer

[英]WebRootPath and TestServer

I have a XUnit test project with the following structure: 我有一个具有以下结构的XUnit测试项目:

  • \\MyApp.Rest \\ MyApp.Rest
    • \\wwwroot \\ wwwroot文件
  • \\MyApp.Rest.Tests \\ MyApp.Rest.Tests
    • Project reference to "MyApp.Rest" 项目引用为“ MyApp.Rest”

_environment.WebRootPath in my integration tests is always null and I have some code in the tested project that depends on that value. 我的集成测试中的_environment.WebRootPath始终为null,并且在测试项目中有一些依赖该值的代码。 I know I can set the value manually in the TestStartup. 我知道我可以在TestStartup中手动设置该值。 I am wondering if there is a better way to set the value other than hard-coding the path ( C:\\...\\MyApp.Rest\\wwwroot )? 我想知道是否有比硬编码路径( 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();
}

I came accross the same issue in my integration test. 我在集成测试中遇到了相同的问题。 I use TestServer class in Microsoft.AspNetCore.TestHost 我在Microsoft.AspNetCore.TestHost使用TestServer

Here is how i configure the WebrootPath : 这是我配置WebrootPath的方法:

_server = new TestServer(new WebHostBuilder()
                .UseWebRoot(@"The path i want to use")
                .UseStartup<Startup>());
_client = _server.CreateClient();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM