我试图了解如何在 ASP.NET Core 6 Web API 控制器上进行集成测试。我尝试遵循我能找到的所有指南、帖子和建议,但由于某种原因,我不断遇到指南中未提及的错误。
\n事件控制器测试.cs
\nnamespace UnitTests.ProjectToBeTested.Controllers\n{\n public class EventControllerTests\n {\n [Fact]\n public async Task EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync()\n {\n\n // Arrange\n var application = new WebApplicationFactory<Program>();\n\n var client = application.CreateClient();\n ...\nRun Code Online (Sandbox Code Playgroud)\n待测试项目.csproj
\n...\n <ItemGroup>\n <InternalsVisibleTo Include="IntegrationTests" />\n </ItemGroup>\n...\nRun Code Online (Sandbox Code Playgroud)\n运行测试时会抛出以下错误:
\n\n消息:\xe2\x80\x89 System.InvalidOperationException:在\n\ 上找不到方法“public static\nIHostBuilder CreateHostBuilder(string[] args)\”或“public static\nIWebHostBuilder CreateWebHostBuilder(string[] args)\” '程序\'。或者,可以扩展 WebApplicationFactory`1,并可以重写\n\n\'CreateHostBuilder\' 或 \'CreateWebHostBuilder\' 以提供你自己的实例。
\n堆栈跟踪:\xe2\x80\x89 WebApplicationFactory
1.CreateWebHostBuilder() WebApplicationFactory1.EnsureServer()\nWebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory1.CreateDefaultClient(Uri baseAddress,\nDelegatingHandler[] 处理程序)\nWebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory1.CreateClient()\nEventControllerTests.EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync()\xe2\x80\x89线\ xe2\x80\x8917\n--- …