using System; using System.Linq.Expressions; using CleanArchitecture.Domain.DomainEvents; using CleanArchitecture.Domain.Enums; using CleanArchitecture.Domain.Interfaces; using CleanArchitecture.Domain.Notifications; using NSubstitute; namespace CleanArchitecture.Domain.Tests; public class CommandHandlerFixtureBase { protected IMediatorHandler Bus { get; } protected IUnitOfWork UnitOfWork { get; } protected DomainNotificationHandler NotificationHandler { get; } protected IUser User { get; } protected CommandHandlerFixtureBase() { Bus = Substitute.For(); UnitOfWork = Substitute.For(); NotificationHandler = Substitute.For(); User = Substitute.For(); User.GetUserId().Returns(Guid.NewGuid()); User.GetUserRole().Returns(UserRole.Admin); UnitOfWork.CommitAsync().Returns(true); } public CommandHandlerFixtureBase VerifyExistingNotification(string errorCode, string message) { Bus.Received(1).RaiseEventAsync( Arg.Is(not => not.Code == errorCode && not.Value == message)); return this; } public CommandHandlerFixtureBase VerifyAnyDomainNotification() { Bus.Received(1).RaiseEventAsync(Arg.Any()); return this; } public CommandHandlerFixtureBase VerifyNoDomainNotification() { Bus.DidNotReceive().RaiseEventAsync(Arg.Any()); return this; } public CommandHandlerFixtureBase VerifyNoRaisedEvent() where TEvent : DomainEvent { Bus.DidNotReceive().RaiseEventAsync(Arg.Any()); return this; } public CommandHandlerFixtureBase VerifyNoRaisedEvent(Expression> checkFunction) where TEvent : DomainEvent { Bus.DidNotReceive().RaiseEventAsync(Arg.Is(checkFunction)); return this; } public CommandHandlerFixtureBase VerifyNoCommit() { UnitOfWork.DidNotReceive().CommitAsync(); return this; } public CommandHandlerFixtureBase VerifyCommit() { UnitOfWork.Received(1).CommitAsync(); return this; } public CommandHandlerFixtureBase VerifyRaisedEvent(Expression> checkFunction) where TEvent : DomainEvent { Bus.Received(1).RaiseEventAsync(Arg.Is(checkFunction)); return this; } }