c# nUnit,日志模拟验证在代码中多次调用的地方不起作用

c# nUnit, log mock verify doesn't work where is called for multiple time in the code

我正在进行 .NET CORE 3.1 nUnit 测试并尝试验证日志。我注意到调用单次的日志确实可以识别和工作,如果相同的登录循环例如 log.logDebug when record does not exist 不适用于验证码。

log 是 ILogger mock 的实例

class

log.LogDebug("No files discovered in testing....");

测试

_loggerMoq.Verify(
            x => x.Log(
                LogLevel.Debug,
                It.IsAny<EventId>(),
                It.Is<It.IsAnyType>((o, t) => string.Equals("No files discovered in testing....", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
                It.IsAny<Exception>(),
                It.IsAny<Func<It.IsAnyType, Exception, string>>()),
            Times.Once);

您必须将 It.IsAny<Func<It.IsAnyType, Exception, string>>()), 更改为 (Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),

不确定为什么会这样,这是 Moq 库中的一个已知错误。

_loggerMoq.Verify(
            x => x.Log(
                LogLevel.Debug,
                It.IsAny<EventId>(),
                It.Is<It.IsAnyType>((o, t) => string.Equals("No files discovered in testing....", o.ToString(), StringComparison.InvariantCultureIgnoreCase)),
                It.IsAny<Exception>(),
                (Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
            Times.AtLeastOnce());