0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-07-01 02:52:56 +00:00

Fix integration tests

This commit is contained in:
Alexander Konietzko 2023-07-01 17:17:05 +02:00
parent 5fb2752e5c
commit d6eb9d10f3
No known key found for this signature in database
GPG Key ID: BA6905F37AEC2B5B
3 changed files with 26 additions and 32 deletions

View File

@ -51,6 +51,22 @@ builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblies(typeof(P
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
ApplicationDbContext appDbContext = services.GetRequiredService<ApplicationDbContext>();
EventStoreDbContext storeDbContext = services.GetRequiredService<EventStoreDbContext>();
DomainNotificationStoreDbContext domainStoreDbContext = services.GetRequiredService<DomainNotificationStoreDbContext>();
appDbContext.EnsureMigrationsApplied();
if (app.Environment.EnvironmentName != "Integration")
{
storeDbContext.EnsureMigrationsApplied();
domainStoreDbContext.EnsureMigrationsApplied();
}
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
@ -62,25 +78,13 @@ app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapHealthChecks("/healthz", new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
app.MapControllers();
app.MapGrpcService<UsersApiImplementation>();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var appDbContext = services.GetRequiredService<ApplicationDbContext>();
var storeDbContext = services.GetRequiredService<EventStoreDbContext>();
var domainStoreDbContext = services.GetRequiredService<DomainNotificationStoreDbContext>();
appDbContext.EnsureMigrationsApplied();
storeDbContext.EnsureMigrationsApplied();
domainStoreDbContext.EnsureMigrationsApplied();
}
app.Run();
// Needed for integration tests web application factory

View File

@ -12,13 +12,9 @@ public class TestFixtureBase
{
public TestFixtureBase()
{
var projectDir = Directory.GetCurrentDirectory();
var configPath = Path.Combine(projectDir, "appsettings.Integration.json");
Factory = new CleanArchitectureWebApplicationFactory(
SeedTestData,
RegisterCustomServicesHandler,
configPath);
RegisterCustomServicesHandler);
ServerClient = Factory.CreateClient();
ServerClient.Timeout = TimeSpan.FromMinutes(5);

View File

@ -23,25 +23,19 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto
private readonly AddCustomSeedDataHandler? _addCustomSeedDataHandler;
private readonly SqliteConnection _connection = new("DataSource=:memory:");
private readonly string? _environment;
private readonly RegisterCustomServicesHandler? _registerCustomServicesHandler;
public CleanArchitectureWebApplicationFactory(
AddCustomSeedDataHandler? addCustomSeedDataHandler,
RegisterCustomServicesHandler? registerCustomServicesHandler,
string? environment = null)
RegisterCustomServicesHandler? registerCustomServicesHandler)
{
_addCustomSeedDataHandler = addCustomSeedDataHandler;
_registerCustomServicesHandler = registerCustomServicesHandler;
_environment = environment;
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
if (!string.IsNullOrWhiteSpace(_environment))
{
builder.UseEnvironment(_environment);
}
builder.UseEnvironment("Integration");
base.ConfigureWebHost(builder);
@ -53,14 +47,14 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto
services.SetupTestDatabase<EventStoreDbContext>(_connection);
services.SetupTestDatabase<DomainNotificationStoreDbContext>(_connection);
var sp = services.BuildServiceProvider();
ServiceProvider sp = services.BuildServiceProvider();
using var scope = sp.CreateScope();
var scopedServices = scope.ServiceProvider;
using IServiceScope scope = sp.CreateScope();
IServiceProvider scopedServices = scope.ServiceProvider;
var applicationDbContext = scopedServices.GetRequiredService<ApplicationDbContext>();
var storeDbContext = scopedServices.GetRequiredService<EventStoreDbContext>();
var domainStoreDbContext = scopedServices.GetRequiredService<DomainNotificationStoreDbContext>();
ApplicationDbContext applicationDbContext = scopedServices.GetRequiredService<ApplicationDbContext>();
EventStoreDbContext storeDbContext = scopedServices.GetRequiredService<EventStoreDbContext>();
DomainNotificationStoreDbContext domainStoreDbContext = scopedServices.GetRequiredService<DomainNotificationStoreDbContext>();
applicationDbContext.EnsureMigrationsApplied();