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

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;
public class WebAppFactoryFixture : WebApplicationFactory<Startup>,
IDisposable
public class WebAppFactoryFixture : WebApplicationFactory<Startup>
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
@ -24,15 +23,20 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>,
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()
.AddJsonFile("appsettings.Tests.json")
.Build();
var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>();
var connectionString = dbConnection?.GetConnectionString();
var dbConnection = configuration.GetSection("DbConnection").Get<DbConnection>()!;
connectionString = dbConnection.GetConnectionString();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
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 scopedServices = scope.ServiceProvider;
var dbContext = scopedServices.GetRequiredService<AsbCloudDbContext>();
var dbContext = new AsbCloudDbContext(
new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql(connectionString)
.Options);
dbContext.Database.EnsureDeleted();
await dbContext.Database.EnsureDeletedAsync();
}
public T GetAuthorizedHttpClient<T>(string uriSuffix)