小编r .*_* .r的帖子

ASP.NET Core 6 Web API 的集成测试抛出 System.InvalidOperationException

我试图了解如何在 ASP.NET Core 6 Web API 控制器上进行集成测试。我尝试遵循我能找到的所有指南、帖子和建议,但由于某种原因,我不断遇到指南中未提及的错误。

\n

事件控制器测试.cs

\n
namespace 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            ...\n
Run Code Online (Sandbox Code Playgroud)\n

待测试项目.csproj

\n
...\n    <ItemGroup>\n        <InternalsVisibleTo Include="IntegrationTests" />\n    </ItemGroup>\n...\n
Run 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()\nWebApplicationFactory 1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory1.CreateDefaultClient(Uri baseAddress,\nDelegatingHandler[] 处理程序)\nWebApplicationFactory 1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory1.CreateClient()\nEventControllerTests.EventController_Post_RespondsOkIfRequestContainsCorrectFeilds_SuccessAsync()\xe2\x80\x89线\ xe2\x80\x8917\n--- …

c# xunit asp.net-core .net-6.0

7
推荐指数
1
解决办法
6728
查看次数

集成测试给出编译错误 CS0051

我正在使用 ASP.NET 6 Core 并为使用模拟DBContext. 尝试以Microsoft 文档为基础。但我无法建造,因为我正在得到

CS0051 可访问性不一致:参数类型“CustomWebApplicationFactory<Program>”比方法“ProjectsControllerTests.ProjectsControllerTests(CustomWebApplicationFactory<Program>)”的可访问性较差

但他们都是公开的?!

项目控制器测试.cs

public class ProjectsControllerTests
    {
        private readonly CustomWebApplicationFactory<Program> _factory;
        private const string s_apiBaseUri = "/api/v1";

        public ProjectsControllerTests(CustomWebApplicationFactory<Program> factory)
        {
            // Arrange
            _factory = factory;
        }

        [Fact]
        public async Task Post_Responds201IfAuthenticatedAndRequiredFields_SuccessAsync()
        {
            // Arrange
            HttpClient? client = _factory.CreateClient(new WebApplicationFactoryClientOptions()
            {
                AllowAutoRedirect = false
            });

            var project = new Project() { Name = "Test Project" };
            var httpContent = new StringContent(JsonSerializer.Serialize(project));

            // Act
            var response = await client.PostAsync($"{s_apiBaseUri}/projects", …
Run Code Online (Sandbox Code Playgroud)

c# integration-testing asp.net-core asp.net-core-webapi

4
推荐指数
1
解决办法
1053
查看次数