Provides NSubstitute for Microsoft.Extenstions.Logging.ILogger
that can be use with .Received()
Instead of Substitute.For<ILogger>()
, use new LoggerSubstitute.Create()
. This returns a Substitute which can be used in the normal NSubstitute way, for example:
var logger = LoggerSubstitute.Create();
SomeMethodThatLogsAnError(logger);
logger.Received().LogError("some message");
or more complex cases
logger.Received().Log(Arg.Is<LogLevel>(level => level > LogLevel.Warning), Arg.Is<string>(s => s.Contains("expected message content"));
logger.DidNotReceive().LogError(Arg.Any<string>());
DidNotReceiveWithAnyArgs()
can't be used with the Log
Level (LogError
, LogWarning
, etc.) methods. Instead, either use DidNotReceive()
with Arg.Any<>()
or use DidNotReceiveWithAnyArgs().Log(
The message is presented already formatted, so checks are made against the formatted output message, not against the parameters. So if your code under test is:
_logger.LogError("Error code: {0}", 5);
this would be checked with
loggerSubstitute.Received().LogError("Error code: 5");