Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/atwayne/test-logger
A utility class to assert `ILogger<T>` calls in unit tests
https://github.com/atwayne/test-logger
Last synced: 12 days ago
JSON representation
A utility class to assert `ILogger<T>` calls in unit tests
- Host: GitHub
- URL: https://github.com/atwayne/test-logger
- Owner: atwayne
- License: mit
- Created: 2021-05-11T20:03:58.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-08-30T04:55:34.000Z (about 2 years ago)
- Last Synced: 2023-08-05T12:11:31.614Z (over 1 year ago)
- Language: C#
- Size: 13.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Dubstep.TestUtilities.TestLogger
Create a `Microsoft.Extensions.Logging.ILogger` instance and assert log calls.
By default, `LogXXX` methods (e.g. `LogDebug`, `LogException`) are non-virtual members of a `ILogger` class, making it difficult to substituted. [Link](https://github.com/nsubstitute/NSubstitute.Analyzers/blob/master/documentation/rules/NS1001.md)
This package is created to make it eaiser to verify your log calls in unit tests. It holds a list of all the log statements and expose the list so you can perform asserts on it.
`LogStatements` exposes the entire list while `LastStatement` expose the last call.
### Usage
[![Dubstep.TestUtilities.TestLogger on fuget.org](https://www.fuget.org/packages/Dubstep.TestUtilities.TestLogger/badge.svg)](https://www.fuget.org/packages/Dubstep.TestUtilities.TestLogger)```bash
dotnet add package Dubstep.TestUtilities.TestLogger
``````csharp
[Test]
public void Should_Log_ExpectedMessage()
{
// Arrange
var loggerStub = new TestLogger();
var service = new DubstepService(loggerStub);
var expectedMessage = "Task Completed."// Act
service.DoSomethingAndLog();
// loggerStub.LogDebug("Task Completed.");// Assert
Assert.AreEqual(LogLevel.Debug, loggerStub.LastStatement.Level);
Assert.AreEqual(expectedMessage, loggerStub.LastStatement.Message);
// Or check `loggerStub.LogStatements` for complex asserts
}[Test]
public void Should_Log_Exception()
{
// Arrange
var loggerStub = new TestLogger();
var service = new DubstepService(loggerStub);
var expectedMessage = "Failed."// Act
service.DoSomethingAndLogException();
// loggerStub.LogError(exception, "Failed.");// Assert
Assert.AreEqual(LogLevel.Error, loggerStub.LastStatement.Level);
Assert.AreEqual(expectedMessage, loggerStub.LastStatement.Message);
var exception = loggerStub.LastStatement.Exception;
// add some asserts on the exception. `Type`, `Message`, `InnerException` etc.
}
```