mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-07-02 03:22:57 +00:00
Fix integration tests
This commit is contained in:
parent
5fb2752e5c
commit
d6eb9d10f3
@ -51,6 +51,22 @@ builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblies(typeof(P
|
|||||||
|
|
||||||
var app = builder.Build();
|
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())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
@ -62,25 +78,13 @@ app.UseHttpsRedirection();
|
|||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapControllers();
|
|
||||||
app.MapHealthChecks("/healthz", new HealthCheckOptions
|
app.MapHealthChecks("/healthz", new HealthCheckOptions
|
||||||
{
|
{
|
||||||
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
||||||
});
|
});
|
||||||
|
app.MapControllers();
|
||||||
app.MapGrpcService<UsersApiImplementation>();
|
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();
|
app.Run();
|
||||||
|
|
||||||
// Needed for integration tests web application factory
|
// Needed for integration tests web application factory
|
||||||
|
@ -12,13 +12,9 @@ public class TestFixtureBase
|
|||||||
{
|
{
|
||||||
public TestFixtureBase()
|
public TestFixtureBase()
|
||||||
{
|
{
|
||||||
var projectDir = Directory.GetCurrentDirectory();
|
|
||||||
var configPath = Path.Combine(projectDir, "appsettings.Integration.json");
|
|
||||||
|
|
||||||
Factory = new CleanArchitectureWebApplicationFactory(
|
Factory = new CleanArchitectureWebApplicationFactory(
|
||||||
SeedTestData,
|
SeedTestData,
|
||||||
RegisterCustomServicesHandler,
|
RegisterCustomServicesHandler);
|
||||||
configPath);
|
|
||||||
|
|
||||||
ServerClient = Factory.CreateClient();
|
ServerClient = Factory.CreateClient();
|
||||||
ServerClient.Timeout = TimeSpan.FromMinutes(5);
|
ServerClient.Timeout = TimeSpan.FromMinutes(5);
|
||||||
|
@ -23,25 +23,19 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto
|
|||||||
private readonly AddCustomSeedDataHandler? _addCustomSeedDataHandler;
|
private readonly AddCustomSeedDataHandler? _addCustomSeedDataHandler;
|
||||||
|
|
||||||
private readonly SqliteConnection _connection = new("DataSource=:memory:");
|
private readonly SqliteConnection _connection = new("DataSource=:memory:");
|
||||||
private readonly string? _environment;
|
|
||||||
private readonly RegisterCustomServicesHandler? _registerCustomServicesHandler;
|
private readonly RegisterCustomServicesHandler? _registerCustomServicesHandler;
|
||||||
|
|
||||||
public CleanArchitectureWebApplicationFactory(
|
public CleanArchitectureWebApplicationFactory(
|
||||||
AddCustomSeedDataHandler? addCustomSeedDataHandler,
|
AddCustomSeedDataHandler? addCustomSeedDataHandler,
|
||||||
RegisterCustomServicesHandler? registerCustomServicesHandler,
|
RegisterCustomServicesHandler? registerCustomServicesHandler)
|
||||||
string? environment = null)
|
|
||||||
{
|
{
|
||||||
_addCustomSeedDataHandler = addCustomSeedDataHandler;
|
_addCustomSeedDataHandler = addCustomSeedDataHandler;
|
||||||
_registerCustomServicesHandler = registerCustomServicesHandler;
|
_registerCustomServicesHandler = registerCustomServicesHandler;
|
||||||
_environment = environment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(_environment))
|
builder.UseEnvironment("Integration");
|
||||||
{
|
|
||||||
builder.UseEnvironment(_environment);
|
|
||||||
}
|
|
||||||
|
|
||||||
base.ConfigureWebHost(builder);
|
base.ConfigureWebHost(builder);
|
||||||
|
|
||||||
@ -53,14 +47,14 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto
|
|||||||
services.SetupTestDatabase<EventStoreDbContext>(_connection);
|
services.SetupTestDatabase<EventStoreDbContext>(_connection);
|
||||||
services.SetupTestDatabase<DomainNotificationStoreDbContext>(_connection);
|
services.SetupTestDatabase<DomainNotificationStoreDbContext>(_connection);
|
||||||
|
|
||||||
var sp = services.BuildServiceProvider();
|
ServiceProvider sp = services.BuildServiceProvider();
|
||||||
|
|
||||||
using var scope = sp.CreateScope();
|
using IServiceScope scope = sp.CreateScope();
|
||||||
var scopedServices = scope.ServiceProvider;
|
IServiceProvider scopedServices = scope.ServiceProvider;
|
||||||
|
|
||||||
var applicationDbContext = scopedServices.GetRequiredService<ApplicationDbContext>();
|
ApplicationDbContext applicationDbContext = scopedServices.GetRequiredService<ApplicationDbContext>();
|
||||||
var storeDbContext = scopedServices.GetRequiredService<EventStoreDbContext>();
|
EventStoreDbContext storeDbContext = scopedServices.GetRequiredService<EventStoreDbContext>();
|
||||||
var domainStoreDbContext = scopedServices.GetRequiredService<DomainNotificationStoreDbContext>();
|
DomainNotificationStoreDbContext domainStoreDbContext = scopedServices.GetRequiredService<DomainNotificationStoreDbContext>();
|
||||||
|
|
||||||
applicationDbContext.EnsureMigrationsApplied();
|
applicationDbContext.EnsureMigrationsApplied();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user