Last active
October 13, 2021 15:49
-
-
Save gsoulavy/56dda58e69e0b16293bed4eb6a6095c5 to your computer and use it in GitHub Desktop.
MockLogger for assertions of Microsoft.Extensions.Logger
This file contains hidden or 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
public abstract class MockLogger<T> : ILogger<T> | |
{ | |
void ILogger.Log<TState>( | |
LogLevel logLevel, | |
EventId eventId, | |
TState state, | |
Exception exception, | |
Func<TState, Exception, string> formatter) | |
=> Log(logLevel, formatter(state, exception)); | |
public abstract void Log(LogLevel logLevel, string message); | |
public virtual bool IsEnabled(LogLevel logLevel) => true; | |
public abstract IDisposable BeginScope<TState>(TState state); | |
} | |
// Asserts | |
/* | |
MockLogger<T> logger = Substitute.For<MockLogger<T>>(); | |
new UnderTest(logger).DoSomething(); | |
logger.Received().Log(Arg.Any<LogLevel>(), Arg.Is<string>(s => s.Contains("some log message"))); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment