0
respostas

Mesmo utilizando a correção do struc não funciona

Message: System.ArgumentException : Object of type 'System.Func3[Microsoft.Extensions.Logging.FormattedLogValues,System.Exception,System.String]' cannot be converted to type 'System.Func3[System.Object,System.Exception,System.String]'.

sem correção do strunc Message: Assert.Contains() Failure Not found: Usar Moq para aprofundar conhecimento de API In value:

delegate void CapturaMensagemLog(LogLevel level, EventId eventId, object state, Exception exception, Func<object, Exception, string> function);

    [Fact]
    public void DadaTarefaComInfoValidasDeveLogar()
    {
        //arrange
        var tituloTarefaEsperado = "Usar Moq para aprofundar conhecimento de API";
        var comando = new CadastraTarefa(tituloTarefaEsperado, new Categoria(100, "Estudo"), new DateTime(2019, 12, 31));

        var mockLogger = new Mock<ILogger<CadastraTarefaHandler>>();

        LogLevel levelCapturado = LogLevel.Debug;
        string mensagemCapturada = string.Empty;

        CapturaMensagemLog captura = (level, eventId, state, exception, func) =>
        {
            levelCapturado = level;
            mensagemCapturada = func(state, exception);
        };

        mockLogger.Setup(l =>
            l.Log(
                It.IsAny<LogLevel>(), //nível de log => LogError
                It.IsAny<EventId>(), //identificador do evento
                It.IsAny<object>(), //objeto que será logado
                It.IsAny<Exception>(),    //exceção que será logada
                //It.IsAny<Func<object, Exception, string>>() //função que converte objeto+exceção >> string)                    
                (Func<object, Exception, string>)It.IsAny<object>()                    
            )).Callback(captura);

        var mock = new Mock<IRepositorioTarefas>();

        var handler = new CadastraTarefaHandler(mock.Object, mockLogger.Object);

        //act
        handler.Execute(comando); //SUT >> CadastraTarefaHandlerExecute

        //assert
        Assert.Equal(LogLevel.Debug, levelCapturado);
        Assert.Contains(tituloTarefaEsperado, mensagemCapturada);
    }

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software