Фикс удаления БД

This commit is contained in:
Степанов Дмитрий 2024-03-13 14:34:51 +03:00
parent caf2392f45
commit 5b3664d3e0

View File

@ -12,8 +12,7 @@ using AsbCloudWebApi.IntegrationTests.Converters;
namespace AsbCloudWebApi.IntegrationTests; namespace AsbCloudWebApi.IntegrationTests;
public class WebAppFactoryFixture : WebApplicationFactory<Startup>, public class WebAppFactoryFixture : WebApplicationFactory<Startup>
IDisposable
{ {
private static readonly JsonSerializerOptions JsonSerializerOptions = new() private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{ {
@ -24,15 +23,20 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>,
private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions));
protected override void ConfigureWebHost(IWebHostBuilder builder) private readonly string connectionString;
public WebAppFactoryFixture()
{ {
var configuration = new ConfigurationBuilder() var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.Tests.json") .AddJsonFile("appsettings.Tests.json")
.Build(); .Build();
var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>(); var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>()!;
var connectionString = dbConnection?.GetConnectionString(); connectionString = dbConnection.GetConnectionString();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services => builder.ConfigureServices(services =>
{ {
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<AsbCloudDbContext>)); var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<AsbCloudDbContext>));
@ -55,13 +59,14 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>,
}); });
} }
public new void Dispose() public override async ValueTask DisposeAsync()
{ {
using var scope = Services.CreateScope(); var dbContext = new AsbCloudDbContext(
var scopedServices = scope.ServiceProvider; new DbContextOptionsBuilder<AsbCloudDbContext>()
var dbContext = scopedServices.GetRequiredService<AsbCloudDbContext>(); .UseNpgsql(connectionString)
.Options);
dbContext.Database.EnsureDeleted(); await dbContext.Database.EnsureDeletedAsync();
} }
public T GetAuthorizedHttpClient<T>(string uriSuffix) public T GetAuthorizedHttpClient<T>(string uriSuffix)