简体   繁体   中英

WebRootPath and TestServer

I have a XUnit test project with the following structure:

  • \\MyApp.Rest
    • \\wwwroot
  • \\MyApp.Rest.Tests
    • Project reference to "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. 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();
}

I came accross the same issue in my integration test. I use TestServer class in Microsoft.AspNetCore.TestHost

Here is how i configure the WebrootPath :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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