【问题标题】:placing asp.net 5 tests in separate assembly将 asp.net 5 测试放在单独的程序集中
【发布时间】:2023-03-19 13:58:01
【问题描述】:

我使用 Microsoft.AspNet.TestHost 来托管 xunit 集成测试。只要测试与 asp.net-5-solution 在同一个项目中,一切都会正常运行。
但我想将测试放在一个单独的程序集中,将它们与解决方案分开。但是当我尝试在单独的解决方案中运行测试时出现错误,TestServer 找不到视图。

Bsoft.Buchhaltung.Tests.LoginTests.SomeTest [FAIL]
  System.InvalidOperationException : The view 'About' was not found. The following locations were searched:
  /Views/Home/About.cshtml
  /Views/Shared/About.cshtml.

我猜 TestServer 正在寻找相对于本地目录的视图。我怎样才能让它在正确的项目路径中查找?

【问题讨论】:

  • 你找到解决办法了吗
  • 不,我仍然需要解决方案:(

标签: unit-testing asp.net-core xunit


【解决方案1】:

我在这里有一个示例 repo - https://github.com/mattridgway/ASPNET5-MVC6-Integration-Tests,它显示了修复(感谢 David Fowler)。

TL;DR - 设置 TestServer 时,您需要设置应用程序基本路径以查看其他项目,以便它可以找到视图。

【讨论】:

    【解决方案2】:

    Matt Ridgway 的答案是在 RC1 是当前版本时编写的。现在(RTM 1.0.0 / 1.0.1)这变得更简单了:

    public class TenantTests
    {
        private readonly TestServer _server;
        private readonly HttpClient _client;
    
        public TenantTests()
        {
            _server = new TestServer(new WebHostBuilder()
                    .UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "..", "..", "..", "SaaSDemo.Web")))
                    .UseEnvironment("Development")
                    .UseStartup<Startup>());
            _client = _server.CreateClient();
    
        }
    
        [Fact]
        public async Task DefaultPageExists()
        {
            var response = await _client.GetAsync("/");
    
            response.EnsureSuccessStatusCode();
    
            var responseString = await response.Content.ReadAsStringAsync();
    
            Assert.True(!string.IsNullOrEmpty(responseString));
    
        }
    }
    

    这里的重点是.UseContentRoot(Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "..", "..", "..", "SaaSDemo.Web")))

    ApplicationBasePath 位于您的测试程序集 bin/debug/{platform-version}/{os-buildarchitecture}/ 文件夹中。您需要向上遍历该树,直到到达包含视图的项目。就我而言。 SaasDemo.TestsSaasDemo.Web 位于同一文件夹中,因此向上遍历 5 个文件夹是正确的。

    【讨论】:

    • 它对我不起作用...我使用 1.1 版。它说“缺少一个或多个编译引用”。建议 可能的原因包括“buildOptions”下缺少“preserveCompilationContext”属性;在应用程序的 project.json 中。但它是在我的 .csproj 文件中设置的。
    【解决方案3】:

    为了将来参考,请注意您现在可以像这样设置内容根:

    string contentRoot = "path/to/your/web/project";
    IWebHostBuilder hostBuilder = new WebHostBuilder()
        .UseContentRoot(contentRoot)
        .UseStartup<Startup>();
    _server = new TestServer(hostBuilder);
    _client = _server.CreateClient();
    

    【讨论】:

    • 在单独的程序集中用 test 进行了测试,我的路径是相对的,效果也很好。谢谢:_server = new TestServer(new WebHostBuilder() .UseContentRoot(@"..\..\..\..\My.Real.Api") .UseStartup());
    【解决方案4】:

    为了使重写的启动类正常工作,我必须做的另一项更改是将 IHostingEnvironment 对象中的 ApplicationName 设置为 Web 项目的实际名称(Web 程序集的名称)。

    public TestStartup(IHostingEnvironment env) : base(env)
            {
                env.ApplicationName = "Demo.Web";
            }
    

    当 TestStartup 位于不同的程序集中并覆盖原始 Startup 类时,这是必需的。就我而言,UseContentRoot 仍然是必需的。

    如果没有设置名称,我总是得到一个 404 未找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-28
      • 2014-05-08
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多