【发布时间】:2018-11-12 20:44:32
【问题描述】:
我有一个 ASP.NET Core 项目,其中包含一些简单的 Razor 页面和一个 Web API 控制器。
我使用Clean Architecture 作为起点。我重命名了项目名称,删除了 MVC 内容并添加了一些我自己的代码。一切运行正常。
但是,集成测试在调用factory.CreateClient() 时会抛出以下错误:
Test Name: ToDo.Tests.Integration.Web.HomeControllerIndexShould.ReturnViewWithCorrectMessage
Test FullName: ToDo.Tests.Integration.Web.HomeControllerIndexShould.ReturnViewWithCorrectMessage
Test Source: C:\Source\Utopia\tests\ToDo.Tests\Integration\Web\HomeControllerIndexShould.cs : line 18
Test Outcome: Failed
Test Duration: 0:00:00,001
Result StackTrace:
at Microsoft.AspNetCore.Hosting.Internal.HostingEnvironmentExtensions.Initialize(IHostingEnvironment hostingEnvironment, String contentRootPath, WebHostOptions options)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder, IFeatureCollection featureCollection)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateServer(IWebHostBuilder builder)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
at ToDo.Tests.Integration.Web.HomeControllerIndexShould..ctor(CustomWebApplicationFactory`1 factory) in C:\Source\Utopia\tests\ToDo.Tests\Integration\Web\HomeControllerIndexShould.cs:line 14
Result Message:
System.ArgumentException : The content root 'C:\Source\Utopia\ToDo.Web' does not exist.
Parameter name: contentRootPath
我尝试使用builder.UseContentRoot 和builder.UseSolutionRelativeContentRoot 配置自定义WebApplicationFactory,但无论我对ContentRoot 方法使用什么值,它都会抛出相同的错误。
我不知道为什么我的测试失败,而 Clean Architecture 示例中的测试正在运行。我也不知道怎么解决。
任何指针都非常感谢!
【问题讨论】:
-
不知何故,我只是自己在项目中遇到了这个问题。我将接受的答案的解决方案添加到 CleanArchitecture 的 3.1 更新中,它似乎已经修复了它。
-
@ssmith 我确实想知道是什么引发了这个问题......不知何故,您的 2.2 Clean Architecture 和 3.1/我的代码之间发生了一些变化。
-
如果你以后遇到问题,也请在 repo 上提出问题!谢谢!
-
当我尝试将测试项目从一个解决方案复制到另一个解决方案时,这发生在我身上。我的测试项目名称中的命名空间,它包含目录、代码文件,到处都必须完美对齐。
标签: c# asp.net-core integration-testing