Bifrost/tests/Bifrost.Web.Tests/Support/FaultOnSave.cs
2026-11-14 06:48:43 +00:00

16 lines
872 B
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace Bifrost.Web.Tests.Support;
/// <summary>
/// Armed by a test, it throws on the save its predicate picks and lets every other save through: a crash between two writes (the
/// SaveChangesInterceptor seam of AccountMergeLedgerTests). The host's contexts all carry it, so a crash in the saga's own scope is real.
/// </summary>
public sealed class FaultOnSave : SaveChangesInterceptor
{
public Func<DbContext, bool>? When { get; set; }
public override ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default) =>
When is { } when && when(eventData.Context!) ? throw new InvalidOperationException("injected save failure") : ValueTask.FromResult(result);
}