How do you mock ILogger LogInformation

前端 未结 5 1639
太阳男子
太阳男子 2020-12-10 12:59

I have a class that receives an ILogger and I want to mock the LogInformation calls but this is an extension method. How do I make the appropiate setup call for this?

5条回答
  •  一向
    一向 (楼主)
    2020-12-10 13:42

    If you're using Moq >= 4.13, here is a way to mock ILogger:

    logger.Verify(x => x.Log(
        It.IsAny(),
        It.IsAny(),
        It.IsAny(),
        It.IsAny(),
        (Func)It.IsAny()));
    
    
    

    You can change the It.IsAny(), It.IsAny(), and It.IsAny() stubs to be more specific, but using It.IsAnyType is necessary because FormattedLogValues is now internal.

    Reference: TState in ILogger.Log used to be object, now FormattedLogValues

    提交回复
    热议问题