solution files
All checks were successful
.NET Test / test (push) Successful in 6m47s

This commit is contained in:
2025-02-01 20:34:34 +04:00
parent 14937c6964
commit 54ea0925dd
30 changed files with 1388 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
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<TestCommand, TestCommandResult>
{
public ValueTask<TestCommandResult> 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<TestCommandHandler>(lifetime));
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(TestCommandHandler)
&& descriptor.Lifetime == lifetime,
expectedCount: 1
);
}
[Fact]
public void WhenCalledMultipleTimes_ShouldRegisterCommandHandlerOnce()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddCommandHandler<TestCommandHandler>()
.AddCommandHandler<TestCommandHandler>()
.AddCommandHandler<TestCommandHandler>());
services.AddCqrs(opt => opt
.AddCommandHandler<TestCommandHandler>());
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(TestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
public class SecondTestCommand {}
public class SecondTestCommandResult {}
[ExcludeFromCodeCoverage]
public class SecondTestCommandHandler : ICommandHandler<SecondTestCommand, SecondTestCommandResult>
{
public ValueTask<SecondTestCommandResult> Handle(SecondTestCommand command, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
public class ThirdTestCommand {}
public class ThirdTestCommandResult {}
[ExcludeFromCodeCoverage]
public class ThirdTestCommandHandler : ICommandHandler<ThirdTestCommand, ThirdTestCommandResult>
{
public ValueTask<ThirdTestCommandResult> Handle(ThirdTestCommand command, CancellationToken cancellation)
{
throw new NotImplementedException();
}
}
[Fact]
public void WhenCalledMultipleTimes_ShouldRegisterAllCommandHandlers()
{
// Given
ServiceCollection services = new();
// When
services.AddCqrs(opt => opt
.AddCommandHandler<TestCommandHandler>()
.AddCommandHandler<SecondTestCommandHandler>());
services.AddCqrs(opt => opt
.AddCommandHandler<ThirdTestCommandHandler>());
// Then
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<TestCommand, TestCommandResult>)
&& descriptor.ImplementationType == typeof(TestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<SecondTestCommand, SecondTestCommandResult>)
&& descriptor.ImplementationType == typeof(SecondTestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
services.ShouldContain(
elementPredicate: descriptor =>
descriptor.ServiceType == typeof(ICommandHandler<ThirdTestCommand, ThirdTestCommandResult>)
&& descriptor.ImplementationType == typeof(ThirdTestCommandHandler)
&& descriptor.Lifetime == ServiceLifetime.Transient,
expectedCount: 1
);
}
}