using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using MinAttest.Application; using MinAttest.Application.Abstractions; using MinAttest.Application.Common.Behaviors; using MinAttest.Infrastructure.Data; using Xunit; namespace MinAttest.Tests.Integration; public class IntegrationTestFixture : IAsyncLifetime { public IServiceProvider Services { get; private set; } = default!; public string ConnectionString { get; private set; } = string.Empty; public async Task InitializeAsync() { await Server.TestDb.StartAsync(); ConnectionString = Server.TestDb.ConnectionString; var services = new ServiceCollection(); services.AddDbContext(opt => opt.UseSqlServer(ConnectionString)); services.AddScoped(sp => sp.GetRequiredService()); services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(ApplicationAssembly).Assembly)); services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); Services = services.BuildServiceProvider(); // Migrate and seed using var scope = Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); await SeedAsync(db); } public async Task DisposeAsync() { await Server.TestDb.StopAsync(); } private static async Task SeedAsync(AppDbContext db) { if (!db.Persons.Any()) { var personId = Guid.NewGuid(); db.Persons.Add(new MinAttest.Domain.Entities.Person { Id = personId, NationalIdHash = "person-hash-123", Email = "person@example.com" }); var employerId = Guid.NewGuid(); db.Employers.Add(new MinAttest.Domain.Entities.Employer { Id = employerId, OrgNumber = "999999999", Name = "TestBedrift AS" }); await db.SaveChangesAsync(); } } }