This commit is contained in:
2026-01-13 23:21:23 -06:00
parent 334c2fecb5
commit 8c65125b16
8 changed files with 27 additions and 1685 deletions

View File

@@ -1,8 +1,9 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Shogi.Identity;
using Shogi.BackEnd.Identity;
using System.Net.Http.Json;
using System.Text.Json;
@@ -24,29 +25,25 @@ public class AatTestFixture : WebApplicationFactory<Program>, IAsyncLifetime
{
builder.UseEnvironment("Development");
builder.ConfigureServices(services =>
builder.ConfigureTestServices(services =>
{
// Remove all EF Core and database provider related services
var descriptorsToRemove = services
.Where(d => d.ServiceType.Namespace != null &&
(d.ServiceType.Namespace.StartsWith("Microsoft.EntityFrameworkCore") ||
d.ServiceType == typeof(ApplicationDbContext) ||
d.ServiceType == typeof(DbContextOptions<ApplicationDbContext>)))
.ToList();
foreach (var descriptor in descriptorsToRemove)
{
// Remove the existing DbContext registration
var descriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(DbContextOptions<ApplicationDbContext>));
services.Remove(descriptor);
}
if (descriptor != null)
{
services.Remove(descriptor);
}
// Add in-memory database for testing
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseInMemoryDatabase("IntegrationTestDb_" + Guid.NewGuid().ToString());
});
// Ensure the database is created and seeded
var sp = services.BuildServiceProvider();
using var scope = sp.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.EnsureCreated();
});
// Add DbContext with InMemory database
services.AddDbContext<ApplicationDbContext>(options =>
options.UseInMemoryDatabase("IntegrationTestDb_" + Guid.NewGuid().ToString()));
});
}
public async Task InitializeAsync()
@@ -54,6 +51,13 @@ public class AatTestFixture : WebApplicationFactory<Program>, IAsyncLifetime
this.HttpClient = this.CreateClient();
this.OtherHttpClient = this.CreateClient();
// Ensure the in-memory database is created
using (var scope = this.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
await db.Database.EnsureCreatedAsync();
}
await this.SetupTestAccountsAndLogin();
}