繁体   English   中英

WebRootPath和TestServer

[英]WebRootPath and TestServer

我有一个具有以下结构的XUnit测试项目:

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

我的集成测试中的_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();
}

我在集成测试中遇到了相同的问题。 我在Microsoft.AspNetCore.TestHost使用TestServer

这是我配置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