using Microsoft.Extensions.Logging; using NSubstitute; using NSubstitute.Core; namespace Cleanuparr.Infrastructure.Tests.TestHelpers; /// /// Predicates for inspecting ILogger calls recorded by NSubstitute. /// public static class LoggerVerificationExtensions { /// /// Whether the logger received exactly log calls /// at the given level whose message contains the specified text. /// public static bool HasLogContaining( this ILogger logger, LogLevel level, string message, int count = 1) { return GetLogCalls(logger, level, message).Count == count; } /// /// Whether the logger received at least one log call /// at the given level whose message contains the specified text. /// public static bool HasLogContainingAtLeastOnce( this ILogger logger, LogLevel level, string message) { return GetLogCalls(logger, level, message).Count > 0; } /// /// Whether the logger received no log calls /// at the given level whose message contains the specified text. /// public static bool HasNoLogContaining( this ILogger logger, LogLevel level, string message) { return GetLogCalls(logger, level, message).Count == 0; } private static List GetLogCalls(ILogger logger, LogLevel level, string message) { return logger.ReceivedCalls() .Where(c => c.GetMethodInfo().Name == "Log") .Where(c => c.GetArguments().Length > 0 && c.GetArguments()[0] is LogLevel l && l == level) .Where(c => c.GetArguments().Length > 2 && c.GetArguments()[2]?.ToString()?.Contains(message) == true) .ToList(); } }