I have a project that I am implementing the Unit Tests with the Mock Framework, in this project I have implemented CQRS using MediatR.
Follow the test code
public RegistrarMaanaimComandTest()
{
[Fact]
public void RegistrarLocal_RetornoSucesso()
{
var mediatorMock = new Mock<IMediatorHandler>();
var localCommandHandler = new LocalCommandHandler(mediatorMock);
var local = new Local();
var registrarLocal = new RegistrarLocalComando(local);
var localRegistradoComSucessoEvento = new LocalRegistradoComSucessoEvento(nome: nome, sigla: sigla, limite: limite);
mediatorMock b.Setup(b =>.RaiseEvent(localRegistradoComSucessoEvento));
localCommandHandler.Handle(registrarLocal);
}
}
public interface IMediatorHandler
{
Task RaiseEvent<T>(T evento) where T : Event;
Task SendCommand<T>(T comando) where T : Command;
}
public class LocalCommandHandler : CommandHandler,INotificationHandler<RegistrarLocalComando>
{
public LocalCommandHandler(IMediatorHandler mediator)
{
_mediator = mediator;
}
public Task Handle(RegistrarLocalComando message, CancellationToken cancellationToken)
{
var localRegistradoComSucessoEvento = new LocalRegistradoComSucessoEvento(nome: message.Nome, sigla: message.Sigla, limite: message.Limite);
_mediator.RaiseEvent(localRegistradoComSucessoEvento);
return Task.CompletedTask;
}
}
When I run I get the following error:
Moq.MockException HResult = 0x80131500 Message = Expected invocation on the mock once, but was 0 times: m = > m.RaiseEvent (Local Registered with Successful Event)
Configured setups: x = > x.RaiseEvent (Local Registered with Successful Event)
Performed invocations: IMediatorHandler.RaiseEvent (Local Registered with Successful Event)