WebRootPath 和 TestServer [英] WebRootPath and TestServer

查看:15
本文介绍了WebRootPath 和 TestServer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a XUnit test project with the following structure:

  • MyApp.Rest
    • wwwroot
    • 对MyApp.Rest"的项目引用

    _environment.WebRootPath 在我的集成测试中始终为空,并且我在测试项目中有一些依赖于该值的代码.我知道我可以在 TestStartup 中手动设置该值.我想知道除了硬编码路径 (C:...MyApp.Restwwwroot) 之外,是否有更好的方法来设置值?

    _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.Restwwwroot)?

    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 in Microsoft.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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆