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

查看:413
本文介绍了您如何模拟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<LogLevel>()It.IsAny<EventId>()It.IsAny<Exception>()存根更改为更具体,但是必须使用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中的 TState以前是对象,现在是FormattedLogValues

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

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

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