gpt4 book ai didi

c# - 集成测试给出编译错误 CS0051

转载 作者:行者123 更新时间:2023-12-04 14:46:48 25 4
gpt4 key购买 nike

我正在使用 ASP.NET 6 Core 并为使用模拟 DBContext 的 Controller 编写基本集成测试。试图将其基于 Microsoft docs .但我无法构建,因为我得到了

CS0051 Inconsistent accessibility: parameter type 'CustomWebApplicationFactory<Program>' is less accessible than method 'ProjectsControllerTests.ProjectsControllerTests(CustomWebApplicationFactory<Program>)' DatabaseManagerService

但它们都是公开的?!

ProjectControllersTests.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", httpContent);

// Assert
Assert.Equal(System.Net.HttpStatusCode.Created, response.StatusCode);
}
}

CustomWebApplicationFactory.cs

public class CustomWebApplicationFactory<TStartup>
: WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<myDbContext>));

if (descriptor != null)
services.Remove(descriptor);

services.AddDbContext<myDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
});

services.AddAuthentication("Test")
.AddScheme<AuthenticationSchemeOptions, MockAuthHandler>(
"Test", options => { });

var sp = services.BuildServiceProvider();

using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var db = scopedServices.GetRequiredService<myDbContext>();
var logger = scopedServices
.GetRequiredService<ILogger<CustomWebApplicationFactory<Program>>>();

db.Database.EnsureCreated();

try
{
db.EndUsers.Add(new DataAccessLibrary.Models.EndUser() { ObjectIdentifier = "test-oid" });
db.SaveChanges();
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred seeding the " +
"database with test messages. Error: {Message}", ex.Message);
}
}
});
}
}

最佳答案

Program 类在 ASP.NET 6 Core 中不是公共(public)的。根据 https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0#twa您可以通过将以下内容附加到 Program.cs 来公开 Program:

public partial class Program { }

还需要注意的是,上面的测试类必须定义为:

public class ProjectsControllerTests : IClassFixture<CustomWebApplicationFactory<Program>>

让 DI 工作。

关于c# - 集成测试给出编译错误 CS0051,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69882197/

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