test(infrastructure): resolve broadcaster in AppDbContext tests #12

Merged
arnaud.dartois merged 1 commits from fix/config-broadcast-di-tests into main 2026-10-20 21:11:06 +02:00
@@ -5,6 +5,7 @@ using Heimdall.Infrastructure.Data;
using Heimdall.Infrastructure.Data.Interceptors;
using Heimdall.Infrastructure.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Shouldly;
@@ -175,13 +176,16 @@ public class CameraAssignmentServiceTests : IAsyncLifetime
var options = new DbContextOptionsBuilder<AppDbContext>()
.UseNpgsql(source.Database.GetConnectionString()).Options;
var broadcast = Substitute.For<IConfigBroadcastService>();
var services = new ServiceCollection();
services.AddSingleton(broadcast);
using var serviceProvider = services.BuildServiceProvider();
string? observedName = null;
broadcast.NotifyAllAgentsReload(Arg.Any<CancellationToken>()).Returns(async _ =>
{
await using var verify = await _dbFactory.CreateDbContextAsync();
observedName = (await verify.Cameras.FindAsync(cameraId))!.Name;
});
await using (var db = new AppDbContext(options, new Heimdall.Infrastructure.Data.Interceptors.AuditInterceptor(), broadcast))
await using (var db = new AppDbContext(options, new Heimdall.Infrastructure.Data.Interceptors.AuditInterceptor(), serviceProvider))
{
(await db.Cameras.FindAsync(cameraId))!.Update("committed", null, null, null);
await db.SaveChangesAsync();
@@ -189,7 +193,7 @@ public class CameraAssignmentServiceTests : IAsyncLifetime
observedName.ShouldBe("committed");
await broadcast.Received(1).NotifyAllAgentsReload(Arg.Any<CancellationToken>());
broadcast.ClearReceivedCalls();
await using (var db = new AppDbContext(options, new Heimdall.Infrastructure.Data.Interceptors.AuditInterceptor(), broadcast))
await using (var db = new AppDbContext(options, new Heimdall.Infrastructure.Data.Interceptors.AuditInterceptor(), serviceProvider))
{
db.Cameras.Add(new Camera(Guid.NewGuid(), "invalid-site", "rtsp://invalid"));
await Should.ThrowAsync<DbUpdateException>(() => db.SaveChangesAsync());