How to test asp.net core built-in Ilogger

前端 未结 6 1452
无人共我
无人共我 2021-02-02 06:36

I want to verify some logs logged. I am using the asp.net core built-in ILogger, and inject it with the asp.net core built-in DI:

private readonly ILogger<         


        
6条回答
  •  一个人的身影
    2021-02-02 07:14

    As @Nkosi've already said, you can't mock an extension method. What you should mock, is the ILogger.Log method, which LogError calls into. It makes the verification code a bit clunky, but it should work:

    MockLogger.Verify(
        m => m.Log(
            LogLevel.Error,
            It.IsAny(),
            It.Is(v => v.ToString().Contains("CreateInvoiceFailed")),
            It.IsAny(),
            It.IsAny>()
        )
    );
    

    (Not sure if this compiles, but you get the gist)

提交回复
热议问题