using System; using System.Linq.Expressions; using CleanArchitecture.Domain.DomainEvents; using CleanArchitecture.Domain.Enums; using CleanArchitecture.Domain.Interfaces; using CleanArchitecture.Domain.Notifications; using Moq; namespace CleanArchitecture.Domain.Tests; public class CommandHandlerFixtureBase { protected CommandHandlerFixtureBase() { Bus = new Mock(); UnitOfWork = new Mock(); NotificationHandler = new Mock(); User = new Mock(); User.Setup(x => x.GetUserId()).Returns(Guid.NewGuid()); User.Setup(x => x.GetUserRole()).Returns(UserRole.Admin); UnitOfWork.Setup(unit => unit.CommitAsync()).ReturnsAsync(true); } protected Mock Bus { get; } protected Mock UnitOfWork { get; } protected Mock NotificationHandler { get; } protected Mock User { get; } public CommandHandlerFixtureBase VerifyExistingNotification(string errorCode, string message) { Bus.Verify( bus => bus.RaiseEventAsync( It.Is(not => not.Code == errorCode && not.Value == message)), Times.Once); return this; } public CommandHandlerFixtureBase VerifyAnyDomainNotification() { Bus.Verify( bus => bus.RaiseEventAsync(It.IsAny()), Times.Once); return this; } public CommandHandlerFixtureBase VerifyNoDomainNotification() { Bus.Verify( bus => bus.RaiseEventAsync(It.IsAny()), Times.Never); return this; } public CommandHandlerFixtureBase VerifyNoRaisedEvent() where TEvent : DomainEvent { Bus.Verify( bus => bus.RaiseEventAsync(It.IsAny()), Times.Never); return this; } public CommandHandlerFixtureBase VerifyNoRaisedEvent(Expression> checkFunction) where TEvent : DomainEvent { Bus.Verify(bus => bus.RaiseEventAsync(It.Is(checkFunction)), Times.Never); return this; } public CommandHandlerFixtureBase VerifyNoCommit() { UnitOfWork.Verify(unit => unit.CommitAsync(), Times.Never); return this; } public CommandHandlerFixtureBase VerifyCommit() { UnitOfWork.Verify(unit => unit.CommitAsync(), Times.Once); return this; } public CommandHandlerFixtureBase VerifyRaisedEvent(Expression> checkFunction) where TEvent : DomainEvent { Bus.Verify(bus => bus.RaiseEventAsync(It.Is(checkFunction)), Times.Once); return this; } }