Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/NetCore.Utilities.UnitTesting/DatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ public abstract class DatabaseFixture<TContext>
where TContext : DbContext
{
/// <summary>
/// The Checkpoint information used by Respawn to clear the database between test runs
/// The Respawner information used by Respawn to clear the database between test runs
/// </summary>
private readonly Checkpoint _checkpoint;
private Respawner? _respawner;

/// <summary>
/// Options used for creating all database contexts handed out by this fixture.
Expand All @@ -142,6 +142,7 @@ public abstract class DatabaseFixture<TContext>

private WeakReference<ITestOutputHelper> _outputHelper = new(null!);
private readonly DatabaseFixtureLoggingSettings _logSettings;
private readonly RespawnerOptions _respawnerOptions;

/// <summary>
/// Creates an instance of the database fixture. Not normally called by testing code, the lifetime of this class is expected to be managed by xUnit.
Expand All @@ -151,8 +152,8 @@ public abstract class DatabaseFixture<TContext>
/// An action to set up the DbContextOptions used to create the context. If null, it will default to using Sql Server with the connection
/// string provided by the <paramref name="connectionString"/> parameter.
/// </param>
/// <param name="checkpointFunc">
/// A function returning a <see cref="Checkpoint"/> object. If null, a default configuration
/// <param name="respawnerOptionsFunc">
/// A function returning a <see cref="RespawnerOptions"/> object. If null, a default configuration
/// that ignores the EF migrations table will be used.
/// </param>
/// <param name="logging">
Expand All @@ -163,7 +164,7 @@ public abstract class DatabaseFixture<TContext>
protected DatabaseFixture(
string connectionString,
Action<DbContextOptionsBuilder<TContext>>? contextOptionsAction = null,
Func<Checkpoint>? checkpointFunc = null,
Func<RespawnerOptions>? respawnerOptionsFunc = null,
DatabaseFixtureLoggingSettings? logging = null)
{
var optionsBuilder = new DbContextOptionsBuilder<TContext>();
Expand Down Expand Up @@ -192,17 +193,17 @@ protected DatabaseFixture(
throw new InvalidOperationException($"The DbContext type used by the database fixture must have a public constructor that takes a DbContextOptions.");
}

if (checkpointFunc == null)
if (respawnerOptionsFunc == null)
{
_checkpoint = new Checkpoint()
_respawnerOptions = new RespawnerOptions
{
TablesToIgnore = new Table[] { "__EFMigrationsHistory" },
DbAdapter = DbAdapter.SqlServer
};
}
else
{
_checkpoint = checkpointFunc();
_respawnerOptions = respawnerOptionsFunc();
}
}

Expand Down Expand Up @@ -290,7 +291,12 @@ public async Task Reset()
if (conn.State == ConnectionState.Closed)
await conn.OpenAsync();

await _checkpoint.Reset(conn);
if (_respawner == null)
{
_respawner = await Respawner.CreateAsync(conn, _respawnerOptions);
}

await _respawner.ResetAsync(conn);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Respawn" Version="5.0.1" />
<PackageReference Include="Respawn" Version="6.2.1" />
<PackageReference Include="Xunit" Version="2.4.2" />
</ItemGroup>

Expand Down
Loading