diff --git a/OpenNest.Server.Tests/ServerFactory.cs b/OpenNest.Server.Tests/ServerFactory.cs index b114d11..d7d5304 100644 --- a/OpenNest.Server.Tests/ServerFactory.cs +++ b/OpenNest.Server.Tests/ServerFactory.cs @@ -15,6 +15,7 @@ namespace OpenNest.Server.Tests; public sealed class ServerFactory : WebApplicationFactory { private bool _cleanedUp; + private NestDatabase? _database; public ServerFactory(string? databasePath = null) { @@ -33,7 +34,8 @@ public sealed class ServerFactory : WebApplicationFactory builder.ConfigureTestServices(services => { services.RemoveAll(); - services.AddSingleton(_ => new NestDatabase(DatabasePath, pooling: false)); + // Register an externally owned instance; this factory closes it before deleting its file. + services.AddSingleton(_database = new NestDatabase(DatabasePath, pooling: false)); }); } @@ -44,6 +46,7 @@ public sealed class ServerFactory : WebApplicationFactory return; _cleanedUp = true; + _database?.Dispose(); if (Directory.Exists(DirectoryPath)) Directory.Delete(DirectoryPath, recursive: true); }