Skip to content

Provides NSubstitute for Microsoft.Extenstions.Logging.ILogger that can be use with .Received()

License

Notifications You must be signed in to change notification settings

AlexVallat/Extensions.Logging.NSubstitute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extensions.Logging.NSubstitute

Nuget

Provides NSubstitute for Microsoft.Extenstions.Logging.ILogger that can be use with .Received()

Usage

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>());

Limitations

DidNotReceiveWithAnyArgs()can't be used with the LogLevel (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");

About

Provides NSubstitute for Microsoft.Extenstions.Logging.ILogger that can be use with .Received()

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages