Skip to content

Commit

Permalink
more better a la Magnus
Browse files Browse the repository at this point in the history
  • Loading branch information
oskogstad committed Oct 4, 2024
1 parent 926e194 commit a467e2a
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Npgsql;
Expand Down Expand Up @@ -65,12 +64,18 @@ public async Task InitializeAsync()
}

/// <summary>
/// This method lets you configure the IoC container in the integration test.
/// This method lets you configure the IoC container for an integration test.
/// It will be reset to the default configuration after each test.
/// You may only call this method once per test.
/// </summary>
/// <param name="configure"></param>
/// <exception cref="InvalidOperationException">Thrown if the method is called more than once per test.</exception>
public void ConfigureServiceCollection(Action<IServiceCollection> configure)
{
if (_rootProvider != _fixtureRootProvider)
{
throw new InvalidOperationException($"Only one call to {nameof(ConfigureServiceCollection)} is allowed per test.");
}

var serviceCollection = BuildServiceCollection();
configure(serviceCollection);
_rootProvider = serviceCollection.BuildServiceProvider();
Expand Down Expand Up @@ -179,6 +184,7 @@ private static IServiceOwnerNameRegistry CreateServiceOwnerNameRegistrySubstitut
public async Task DisposeAsync()
{
await _rootProvider.DisposeAsync();
await _fixtureRootProvider.DisposeAsync();
await _dbContainer.DisposeAsync();
}

Expand All @@ -201,7 +207,11 @@ public async Task ResetState()
await using var connection = new NpgsqlConnection(_dbContainer.GetConnectionString());
await connection.OpenAsync();
await _respawner.ResetAsync(connection);
_rootProvider = _fixtureRootProvider;
if (_rootProvider != _fixtureRootProvider)
{
await _rootProvider.DisposeAsync();
_rootProvider = _fixtureRootProvider;
}
}

private async Task EnsureDatabaseAsync()
Expand Down

0 comments on commit a467e2a

Please sign in to comment.