gpt4 book ai didi

c# - WebApplicationFactory 抛出 contentRootPath 在 ASP.NET Core 集成测试中不存在的错误

转载 作者:行者123 更新时间:2023-11-30 13:43:40 25 4
gpt4 key购买 nike

我有一个 ASP.NET Core 项目,其中包含一些简单的 Razor 页面和一个 Web API Controller 。

我正在使用 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

我试过配置自定义 WebApplicationFactory通过使用 builder.UseContentRootbuilder.UseSolutionRelativeContentRoot但无论我为 ContentRoot 使用什么值,它都会抛出相同的错误方法。

我不知道为什么我的测试失败了,而 Clean Architecture 示例中的测试正在运行。我也不知道如何解决它。

非常感谢任何指点!

最佳答案

这个方法对我有用

        var client = _factory
.WithWebHostBuilder(builder => builder.UseSolutionRelativeContentRoot("relative/path/of/project/under/test"))
.CreateClient();

How the test infrastructure infers the app content root path说(添加了我的标记)

The WebApplicationFactory constructor infers the app content root path by searching for a WebApplicationFactoryContentRootAttribute on the assembly containing the integration tests with a key equal to the TEntryPoint assembly System.Reflection.Assembly.FullName. In case an attribute with the correct key isn't found, WebApplicationFactory falls back to searching for a solution file (.sln) and appends the TEntryPoint assembly name to the solution directory. The app root directory (the content root path) is used to discover views and content files.

关于c# - WebApplicationFactory 抛出 contentRootPath 在 ASP.NET Core 集成测试中不存在的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53269815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com