gpt4 book ai didi

c# - 在 Xunit 集成测试中检索 ASP.NET Core 3.1 数据库上下文

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

我一直在使用 Xunit 为使用数据库的 .NET Core 3.1 Web 应用程序创建集成测试。为了进行测试,我已从 Microsoft Documentation 切换到内存数据库。 . CustomWebApplicationFactory 代码是:

public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder webHostBuilder)
{
webHostBuilder.ConfigureServices(services =>
{
// Remove the app's database registration.
var serviceDescriptor = services
.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<MyDbContext>));

if (serviceDescriptor != null)
{
services.Remove(serviceDescriptor);
}

// Add MyDbContext using an in-memory database for testing.
services.AddDbContext<MyDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
});

var servicesProvider = services.BuildServiceProvider();

// Create a scope to obtain a reference to the database context (MyDbContext).
using (var serviceScope = servicesProvider.CreateScope())
{
var scopedServices = serviceScope.ServiceProvider;
var db = scopedServices.GetRequiredService<MyDbContext>();
var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();

db.Database.EnsureCreated(); // Ensure the database is created.

try
{
DatabaseSeeding.voidInitialiseCazIdentityProviderDatabase(db); // Seed the database with test data.
}
catch (Exception ex)
{
logger.LogError(ex, $"An error occurred seeding the database with data. Error: {ex.Message}");
}
}
});
}

我的基本页面测试在这种安排下工作得很好,但我现在想检查内存数据库是否已被集成测试修改。无论发生什么,对数据库的引用都不会在 Xunit DI 容器中结束(如果存在这种情况)。我的测试类使用以下代码初始化:
    public class IdpUserServiceTests : IClassFixture<CustomWebApplicationFactory<Startup>>
{
private readonly CustomWebApplicationFactory<Startup> _webApplicationFactory;
private readonly ITestOutputHelper _testOutput;
private readonly HttpClient _httpClient;
private readonly MyDbContext _myDbContext;

public IdpUserServiceTests(CustomWebApplicationFactory<Startup> webApplicationFactory, MyDbContext myDbContext, ITestOutputHelper testOutput)
{
_webApplicationFactory = webApplicationFactory;
_myDbContext = myDbContext;
_testOutput = testOutput;
_httpClient = _webApplicationFactory.CreateClient();
}

//Tests

但在尝试运行测试时,我收到以下错误:
The following constructor parameters did not have matching fixture data: MyDbContext objMyDbContext

我正在寻找访问内存数据库的正确方法——显然不是通过构造函数注入(inject)。我已经尝试过这个答案 - Access in memory dbcontext in integration test - 但事情似乎在 2.2 和 3.1 之间发生了变化。

最佳答案

事实证明,上面引用的集成测试答案中的 Access in memory dbcontext 对于 ASP.NET Core 3.1 来说并不完全正确。 scopeFactory 必须从不同的来源找到:

var scopeFactory = _webApplicationFactory.Services.GetService<IServiceScopeFactory>();
所以测试类现在看起来像:
public class ServiceTests : IClassFixture<CustomWebApplicationFactory<Startup>>
{
private readonly CustomWebApplicationFactory<Startup> _webApplicationFactory;
private readonly ITestOutputHelper _testOutput;
private readonly HttpClient _httpClient;

public ServiceTests(CustomWebApplicationFactory<Startup> webApplicationFactory, ITestOutputHelper testOutput)
{
_webApplicationFactory = webApplicationFactory;
_testOutput = testOutput;
_httpClient = _webApplicationFactory.CreateClient();
}

[Fact]

public async Task AServiceTest()
{
// Do some HTTP client stuff that alters the in-memory database

var scopeFactory = _webApplicationFactory.Services.GetService<IServiceScopeFactory>();

using(var scope = scopeFactory.CreateScope())
{
var myDataContext = scope.ServiceProvider.GetService<MyDataContext>();

// Query the in-memory database
}
}
}

关于c# - 在 Xunit 集成测试中检索 ASP.NET Core 3.1 数据库上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62048264/

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