scm-api / tests /SCM.IntegrationTests /ScmApiFactory.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
1.37 kB
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using Testcontainers.PostgreSql;
using Xunit;
namespace SCM.IntegrationTests;
/// <summary>
/// Sobe a API inteira (TestServer) apontando para um PostgreSQL descartável (Testcontainers).
/// As migrations rodam no startup (ambiente Development), criando schema + seed do admin.
/// </summary>
public sealed class ScmApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
private readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder()
.WithImage("postgres:16")
.Build();
public string ConnectionString => _postgres.GetConnectionString();
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");
builder.ConfigureAppConfiguration((_, config) =>
{
config.AddInMemoryCollection(new Dictionary<string, string?>
{
["ConnectionStrings:Postgres"] = _postgres.GetConnectionString(),
["Migrations:RunOnStartup"] = "true",
});
});
}
async Task IAsyncLifetime.InitializeAsync() => await _postgres.StartAsync();
async Task IAsyncLifetime.DisposeAsync()
{
await _postgres.DisposeAsync();
await base.DisposeAsync();
}
}