Search code examples
c#moqxunit.net-core-3.1xunit.net

Is there a way to verify ILogger <T> instance does not have a LogError?


In the following code, I check for any LogErrors in the ILogger instance. However, I would like to verify that the ILogger instance does not contain any LogError entries. Can you help?

    target.ProcessMainframeJobs(stream, filename, processorlog.Object, testBase.executionContext);
    processorlog.Verify(logger => logger.Log(
            It.Is<LogLevel>(logLevel => logLevel == LogLevel.Error),
            It.Is<EventId>(eventId => eventId.Id == 0),
            It.Is<It.IsAnyType>((@object, @type) => @object.ToString().Contains("Failed")  && @type.Name == "FormattedLogValues"),
            It.IsAny<Exception>(),
            It.IsAny<Func<It.IsAnyType, Exception, string>>()),
        Times.Once);
}

Solution

    • Restrict only the LogLevel and accept anything for any other parameters
    • Check against Times.Never
    loggerMock.Verify(l => l.Log(
      LogLevel.Error, 
      It.IsAny<EventId>(), 
      It.IsAny<It.IsAnyType>(), 
      It.IsAny<Exception>(), 
      (Func<It.IsAnyType, Exception, string>)It.IsAny<object>()), 
      Times.Never);