Skip to content

Commit

Permalink
v1.3.0 (#12)
Browse files Browse the repository at this point in the history
* Fix the WithArguments() assertion message when comparing the number of the arguments.
fixes #10

* Add the support of WithException() followed by a WithArguments() methods.
fixes #11
  • Loading branch information
GillesTourreau authored Oct 24, 2023
1 parent 2cc6b79 commit a38b853
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Logging.Assertions/ILoggerMockSetupSequenceError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public interface ILoggerMockSetupSequenceError : ILoggerMockSetupSequenceLog
/// </summary>
/// <param name="exception">Delegate which allows to analyze the content of the <see cref="Exception"/>.</param>
/// <returns>An instance of <see cref="ILoggerMockSetupSequence"/> which allows to continue the setup of the method calls.</returns>
ILoggerMockSetupSequence WithException(Action<Exception> exception);
ILoggerMockSetupSequenceLog WithException(Action<Exception> exception);
}
}
4 changes: 2 additions & 2 deletions src/Logging.Assertions/LoggerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public ILoggerMockSetupSequence WithArguments(int count, Action<LogMessageTempla
return this;
}

public ILoggerMockSetupSequence WithException(Action<Exception> exception)
public ILoggerMockSetupSequenceLog WithException(Action<Exception> exception)
{
this.exception = exception;

Expand All @@ -236,7 +236,7 @@ public void Assert<TState>(LogLevel logLevel, TState state, Exception? exception

if (stateAsList.Count - 1 != this.arguments.Count)
{
Services.ThrowException($"Incorrect template message argument count for the '{originalMessage.Value}' template message. (Expected: '{this.arguments.Count}', Actual: '{stateAsList.Count}')");
Services.ThrowException($"Incorrect template message argument count for the '{originalMessage.Value}' template message. (Expected: '{this.arguments.Count}', Actual: '{stateAsList.Count - 1}')");
}

var messageArguments = new LogMessageTemplateArguments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static ILoggerMockSetupSequenceLog LogWarning(this ILoggerMockSetupSequen
/// <param name="sequence"><see cref="ILoggerMockSetupSequence"/> to setup the sequence.</param>
/// <param name="expectedException"><see cref="Exception"/> instance expected.</param>
/// <returns>An instance of <see cref="ILoggerMockSetupSequence"/> which allows to continue the setup of the method calls.</returns>
public static ILoggerMockSetupSequence WithException(this ILoggerMockSetupSequenceError sequence, Exception expectedException)
public static ILoggerMockSetupSequenceLog WithException(this ILoggerMockSetupSequenceError sequence, Exception expectedException)
{
return sequence.WithException(actualException => actualException.Should().BeSameAs(expectedException));
}
Expand Down
6 changes: 5 additions & 1 deletion src/Logging.Assertions/Logging.Assertions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.Logging.Assertions</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>
1.3.0
- Fix an assertion message with the `WithArguments()` method.
- Add the support of the `WithException()` followed by the `WithArguments()` method. (fixes #11).

1.2.0
- Improve the BeginScope() to use a delegate to assert complex state objects.
- Add a BeginScopeAsDictionary() method to check the state as dictionary string/object (useful for Application Insights logs).

1.1.0
- Add the support to assert the log message template and the parameters.

Expand Down
16 changes: 10 additions & 6 deletions tests/Logging.Assertions.Tests/LoggerMockTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ public void LogError_WithException_SameObjectReference()
var logger = new LoggerMock<ObjectToLog>();
logger.SetupSequence()
.LogInformation("Log before error")
.LogError("Log Error")
.LogError("Log Error {Id}")
.WithException(exception)
.WithArguments(1234)
.LogTrace("Log Trace after error")
.LogDebug("Log Debug after error")
.LogInformation("Log Information after error")
Expand All @@ -180,8 +181,9 @@ public void LogError_WithException_WithActionDelegate()
var logger = new LoggerMock<ObjectToLog>();
logger.SetupSequence()
.LogInformation("Log before error")
.LogError("Log Error")
.LogError("Log Error {Id}")
.WithException(e => e.Should().BeSameAs(exception))
.WithArguments(1234)
.LogTrace("Log Trace after error")
.LogDebug("Log Debug after error")
.LogInformation("Log Information after error")
Expand All @@ -203,8 +205,9 @@ public void LogError_WithException_ExceptionDifferent()
var logger = new LoggerMock<ObjectToLog>();
logger.SetupSequence()
.LogInformation("Log before error")
.LogError("Log Error")
.LogError("Log Error {Id}")
.WithException(new DivideByZeroException("Other exception"))
.WithArguments(1234)
.LogTrace("Log Trace after error")
.LogDebug("Log Debug after error")
.LogInformation("Log Information after error")
Expand Down Expand Up @@ -280,6 +283,7 @@ public void LogWithMessageTemplate_DelegateAssertion_WrongExpectedArgumentCount(
.LogInformation("Log information with parameters {Id}, {Name} and {Object}")
.WithArguments(100, args =>
{
args.Should().HaveCount(3);
args["Id"].Should().Be(1234);
args["Name"].Should().Be("The name");
args["Object"].Should().BeEquivalentTo(new { Property = "I am object" });
Expand All @@ -290,7 +294,7 @@ public void LogWithMessageTemplate_DelegateAssertion_WrongExpectedArgumentCount(

objectToLog.Invoking(o => o.InvokeWithMessageTemplate())
.Should().ThrowExactly<XunitException>()
.WithMessage("Incorrect template message argument count for the 'Log information with parameters {Id}, {Name} and {Object}' template message. (Expected: '100', Actual: '4')");
.WithMessage("Incorrect template message argument count for the 'Log information with parameters {Id}, {Name} and {Object}' template message. (Expected: '100', Actual: '3')");
}

[Fact]
Expand Down Expand Up @@ -322,7 +326,7 @@ public void LogWithMessageTemplate_ParametersAssertion_WrongExpectedArgumentCoun

objectToLog.Invoking(o => o.InvokeWithMessageTemplate())
.Should().ThrowExactly<XunitException>()
.WithMessage("Incorrect template message argument count for the 'Log information with parameters {Id}, {Name} and {Object}' template message. (Expected: '9', Actual: '4')");
.WithMessage("Incorrect template message argument count for the 'Log information with parameters {Id}, {Name} and {Object}' template message. (Expected: '9', Actual: '3')");
}

[Fact]
Expand Down Expand Up @@ -594,7 +598,7 @@ public void Invoke()
public void InvokeWithException(Exception exception)
{
this.logger.LogInformation("Log before error");
this.logger.LogError(exception, "Log Error");
this.logger.LogError(exception, "Log Error {Id}", 1234);
this.logger.LogTrace("Log Trace after error");
this.logger.LogDebug("Log Debug after error");
this.logger.LogInformation("Log Information after error");
Expand Down

0 comments on commit a38b853

Please sign in to comment.