using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.DependencyInjection; namespace Cqrs.Tests.CqrsServicesExtensionsTests; public class AddCommandHandler { public class TestCommand {} public class TestCommandResult {} [ExcludeFromCodeCoverage] public class TestCommandHandler : ICommandHandler { public ValueTask Handle(TestCommand command, CancellationToken cancellation) { throw new NotImplementedException(); } } [Theory] [InlineData(ServiceLifetime.Transient)] [InlineData(ServiceLifetime.Scoped)] [InlineData(ServiceLifetime.Singleton)] public void WhenCalled_ShouldRegisterCommandHandler(ServiceLifetime lifetime) { // Given ServiceCollection services = new(); // When services.AddCqrs(opt => opt.AddCommandHandler(lifetime)); // Then services.ShouldContain( elementPredicate: descriptor => descriptor.ServiceType == typeof(ICommandHandler) && descriptor.ImplementationType == typeof(TestCommandHandler) && descriptor.Lifetime == lifetime, expectedCount: 1 ); } [Fact] public void WhenCalledMultipleTimes_ShouldRegisterCommandHandlerOnce() { // Given ServiceCollection services = new(); // When services.AddCqrs(opt => opt .AddCommandHandler() .AddCommandHandler() .AddCommandHandler()); services.AddCqrs(opt => opt .AddCommandHandler()); // Then services.ShouldContain( elementPredicate: descriptor => descriptor.ServiceType == typeof(ICommandHandler) && descriptor.ImplementationType == typeof(TestCommandHandler) && descriptor.Lifetime == ServiceLifetime.Transient, expectedCount: 1 ); } public class SecondTestCommand {} public class SecondTestCommandResult {} [ExcludeFromCodeCoverage] public class SecondTestCommandHandler : ICommandHandler { public ValueTask Handle(SecondTestCommand command, CancellationToken cancellation) { throw new NotImplementedException(); } } public class ThirdTestCommand {} public class ThirdTestCommandResult {} [ExcludeFromCodeCoverage] public class ThirdTestCommandHandler : ICommandHandler { public ValueTask Handle(ThirdTestCommand command, CancellationToken cancellation) { throw new NotImplementedException(); } } [Fact] public void WhenCalledMultipleTimes_ShouldRegisterAllCommandHandlers() { // Given ServiceCollection services = new(); // When services.AddCqrs(opt => opt .AddCommandHandler() .AddCommandHandler()); services.AddCqrs(opt => opt .AddCommandHandler()); // Then services.ShouldContain( elementPredicate: descriptor => descriptor.ServiceType == typeof(ICommandHandler) && descriptor.ImplementationType == typeof(TestCommandHandler) && descriptor.Lifetime == ServiceLifetime.Transient, expectedCount: 1 ); services.ShouldContain( elementPredicate: descriptor => descriptor.ServiceType == typeof(ICommandHandler) && descriptor.ImplementationType == typeof(SecondTestCommandHandler) && descriptor.Lifetime == ServiceLifetime.Transient, expectedCount: 1 ); services.ShouldContain( elementPredicate: descriptor => descriptor.ServiceType == typeof(ICommandHandler) && descriptor.ImplementationType == typeof(ThirdTestCommandHandler) && descriptor.Lifetime == ServiceLifetime.Transient, expectedCount: 1 ); } }