using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Xunit; namespace Musify.Application.Tests.TestSupport; /// /// Base class for handler tests: gives every test method its own empty, isolated /// (xUnit creates a new instance of the test class per test method, /// so there is no state to reset between tests) and a no-op logger factory for handlers that /// take an . /// public abstract class HandlerTestBase : IAsyncLifetime { private TestDatabase database = null!; protected TestDatabase Database => database; public async ValueTask InitializeAsync() => database = await TestDatabase.CreateAsync(); public ValueTask DisposeAsync() => database.DisposeAsync(); protected static ILogger NoOpLogger() => NullLogger.Instance; /// Adds the entities and persists them, so handlers under test see committed data. protected async Task SeedAsync(params object[] entities) { foreach (var entity in entities) Database.Add(entity); await Database.SaveChangesAsync(CancellationToken.None); } }