The KISS.Moq.Logger NuGet package contains a set of extension methods to ease verification of Microsoft Logging extensions using Moq.
Verifying extension methods using Moq is not possible due to how the Moq framework and extension method works.
Since the Microsoft Logging extensions heavily relies on extension methods it is cumbersome to compose Verify
operations against ILogger since it requires you to moq the ILogger.Log rather than the extension
methods themselves.
This NuGet package adds a set of verify extension methods called VerifyExt that makes it possible to
verify the ILogger extension methods in the same way as other methods.
The usage is simple:
-
Install the latest version of
Kiss.Moq.Loggerusing NuGet. -
Create a
Mockinstance for theILoggerorILogger<TCategoryName>interface you want to mock.Mock<ILogger> loggerMock = new Mock<ILogger>(); -
Execute the code you want to test
loggerMock.Object.LogInformation("Hello {You}", "World"); -
Add a
using statementfor theKISS.Moq.Loggernamespaceusing KISS.Moq.Logger; -
Use the
VerifyExtmethod to verify theMock<ILogger>instance.loggerMock.VerifyExt(logger => logger.LogInformation("Hello {You}", "World");
NOTE: The
VerifyExtis only supported on extension methods in theMicrosoft.Extensions.Logging.LoggerExtensionsextension class.