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.Logger
using NuGet. -
Create a
Mock
instance for theILogger
orILogger<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 statement
for theKISS.Moq.Logger
namespaceusing KISS.Moq.Logger;
-
Use the
VerifyExt
method to verify theMock<ILogger>
instance.loggerMock.VerifyExt(logger => logger.LogInformation("Hello {You}", "World");
NOTE: The
VerifyExt
is only supported on extension methods in theMicrosoft.Extensions.Logging.LoggerExtensions
extension class.