DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/IntegrationTests/TestWebApplicationFactory.cs
Степанов Дмитрий 2aa897d3df Интеграционные тесты
1. Добавил Docker Compose
2. Добавил AppFactory
3. Добавлен базовый класс для интеграционных классов
4. Покрыл тестами контроллер AdminDeposit
2023-12-12 19:13:07 +05:00

46 lines
1.2 KiB
C#

using System.Linq;
using System.Threading.Tasks;
using AsbCloudDb.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;
namespace AsbCloudWebApi.Tests.IntegrationTests;
public class TestWebApplicationFactory : WebApplicationFactory<Startup>,
IAsyncLifetime
{
private readonly PostgreSqlContainer _dbContainer = new PostgreSqlBuilder()
.WithImage("timescale/timescaledb:latest-pg15")
.WithDatabase("AsbCloud")
.WithUsername("postgres")
.WithPassword("postgres")
.Build();
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<AsbCloudDbContext>));
if (descriptor is not null)
services.Remove(descriptor);
services.AddDbContext<AsbCloudDbContext>(options =>
options.UseNpgsql(_dbContainer.GetConnectionString()));
});
}
public Task InitializeAsync()
{
return _dbContainer.StartAsync();
}
public new Task DisposeAsync()
{
return _dbContainer.StopAsync();
}
}