-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace ILogger.Moq NuGet with custom class
- Loading branch information
Showing
2 changed files
with
46 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tests/DevBetterWeb.UnitTests/NSubstituteLoggerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
|
||
namespace DevBetterWeb.UnitTests; | ||
|
||
/// <summary> | ||
/// Extension methods to help verify calls to <see cref="ILogger"/>'s various extension methods | ||
/// that cannot be mocked/verified through normal means. | ||
/// </summary> | ||
internal static class NSubstituteLoggerExtensions | ||
{ | ||
/// <summary> | ||
/// Use when you want to verify that <see cref="LoggerExtensions.LogInformation(Microsoft.Extensions.Logging.ILogger,Microsoft.Extensions.Logging.EventId,System.Exception?,string?,object?[])"/> | ||
/// was called <paramref name="numberOfCalls"/> time(s) and with what message you expect. | ||
/// </summary> | ||
public static void ReceivedLogInformation(this ILogger mockedLogger, int numberOfCalls, string expectedMessage) | ||
{ | ||
mockedLogger.Received(numberOfCalls).Log(LogLevel.Information, Arg.Any<EventId>(), expectedMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Use when you want to verify that <see cref="LoggerExtensions.LogError(Microsoft.Extensions.Logging.ILogger,Microsoft.Extensions.Logging.EventId,System.Exception?,string?,object?[])"/> | ||
/// was called <paramref name="numberOfCalls"/> time(s) and with what excpetion and message you expect. | ||
/// </summary> | ||
public static void ReceivedLogError(this ILogger mockedLogger, int numberOfCalls, Exception expectedException, string expectedMessage) | ||
{ | ||
mockedLogger.Received(numberOfCalls).Log(LogLevel.Error, Arg.Any<EventId>(), expectedException, expectedMessage); | ||
} | ||
} |