你如何模拟 ILogger LogInformation [英] How do you mock ILogger LogInformation

查看:20
本文介绍了你如何模拟 ILogger LogInformation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接收 ILogger 的类,我想模拟 LogInformation 调用,但这是一个扩展方法.我如何为此进行适当的设置调用?

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?

推荐答案

如果您使用的是 Moq >= 4.13,这里是一种模拟 ILogger 的方法:

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

logger.Verify(x => x.Log(
    It.IsAny<LogLevel>(),
    It.IsAny<EventId>(),
    It.IsAny<It.IsAnyType>(),
    It.IsAny<Exception>(),
    (Func<It.IsAnyType, Exception, string>)It.IsAny<object>()));

您可以更改 It.IsAny()It.IsAny()It.IsAny() 存根更具体,但使用 It.IsAnyType 是必要的,因为 FormattedLogValues 现在是 internal.

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

参考:ILogger.Log 中的 TState 以前是对象,现在是 FormattedLogValues

这篇关于你如何模拟 ILogger LogInformation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆