您如何模拟ILogger LogInformation [英] How do you mock 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屋!