[Fact]
public void QuandoLancarExceptionDeveraLogarMessangemExcption()
{
//arrange
var mensagemDeErroEsperada = "Houve um erro na inclusão de tarefas.";
var excecaoEsperada = new Exception(mensagemDeErroEsperada);
var command = new CadastraTarefa("Estudar XUnit", new Categoria("Estudo"), new DateTime(2019, 12, 31));
var mockLogger = new Mock<ILogger<CadastraTarefaHandler>>();
var mockRepositorioTarefas = new Mock<IRepositorioTarefas>();
mockRepositorioTarefas.Setup(r => r.IncluirTarefas(It.IsAny<Tarefa[]>())).Throws(excecaoEsperada);
var repositorioTarefas = mockRepositorioTarefas.Object;
var handler = new CadastraTarefaHandler(repositorioTarefas, mockLogger.Object);
//action
var resultado = handler.Execute(command);
//assert
mockLogger.Verify(l =>
l.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.IsAny<object>(),
excecaoEsperada,
It.IsAny<Func<object, Exception, string>>()
),
Times.Once());
}
Aqui está a Exception do Mock FAK.TEsteIntegracao.Tests.CadastraTarefaHandlerExecute.QuandoLancarExceptionDeveraLogarMessangemExcption Source: CadastraTarefaHandlerExecute.cs line 57 Duration: 144 ms
Message: Moq.MockException : Expected invocation on the mock once, but was 0 times: l => l.Log(), It.IsAny
Performed invocations:
Mock<ILogger<CadastraTarefaHandler>:1> (l):
ILogger.Log<FormattedLogValues>(LogLevel.Debug, 0, Persistindo a tarefa..., null, Func<FormattedLogValues, Exception, string>)
ILogger.Log<FormattedLogValues>(LogLevel.Error, 0, Houve um erro na inclusão de tarefas., System.Exception: Houve um erro na inclusão de tarefas.
at Moq.Behaviors.ThrowException.Execute(Invocation invocation)
at Moq.MethodCall.ExecuteCore(Invocation invocation)
at Moq.Setup.Execute(Invocation invocation)
at Moq.FindAndExecuteMatchingSetup.Handle(Invocation invocation, Mock mock)
at Moq.Mock.Moq.IInterceptor.Intercept(Invocation invocation)
at Moq.CastleProxyFactory.Interceptor.Intercept(IInvocation underlying)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IRepositorioTarefasProxy.IncluirTarefas(Tarefa[] tarefas)
at FAK.TesteIntegracao.Services.Handlers.CadastraTarefaHandler.Execute(CadastraTarefa comando) in D:\GIT_Fak\FAK.TesteIntegracao\Source\FAK.TesteIntegracao.Services\Handlers\CadastraTarefaHandler.cs:line 34, Func<FormattedLogValues, Exception, string>)
Stack Trace:
Mock.Verify(Mock mock, LambdaExpression expression, Times times, String failMessage)
Mock1.Verify(Expression
1 expression, Times times)
CadastraTarefaHandlerExecute.QuandoLancarExceptionDeveraLogarMessangemExcption() line 74