Skip to content

Commit a38b853

Browse files
v1.3.0 (#12)
* 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
1 parent 2cc6b79 commit a38b853

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

src/Logging.Assertions/ILoggerMockSetupSequenceError.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public interface ILoggerMockSetupSequenceError : ILoggerMockSetupSequenceLog
2020
/// </summary>
2121
/// <param name="exception">Delegate which allows to analyze the content of the <see cref="Exception"/>.</param>
2222
/// <returns>An instance of <see cref="ILoggerMockSetupSequence"/> which allows to continue the setup of the method calls.</returns>
23-
ILoggerMockSetupSequence WithException(Action<Exception> exception);
23+
ILoggerMockSetupSequenceLog WithException(Action<Exception> exception);
2424
}
2525
}

src/Logging.Assertions/LoggerMock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public ILoggerMockSetupSequence WithArguments(int count, Action<LogMessageTempla
214214
return this;
215215
}
216216

217-
public ILoggerMockSetupSequence WithException(Action<Exception> exception)
217+
public ILoggerMockSetupSequenceLog WithException(Action<Exception> exception)
218218
{
219219
this.exception = exception;
220220

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

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

242242
var messageArguments = new LogMessageTemplateArguments(

src/Logging.Assertions/LoggerMockSetupSequenceExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static ILoggerMockSetupSequenceLog LogWarning(this ILoggerMockSetupSequen
106106
/// <param name="sequence"><see cref="ILoggerMockSetupSequence"/> to setup the sequence.</param>
107107
/// <param name="expectedException"><see cref="Exception"/> instance expected.</param>
108108
/// <returns>An instance of <see cref="ILoggerMockSetupSequence"/> which allows to continue the setup of the method calls.</returns>
109-
public static ILoggerMockSetupSequence WithException(this ILoggerMockSetupSequenceError sequence, Exception expectedException)
109+
public static ILoggerMockSetupSequenceLog WithException(this ILoggerMockSetupSequenceError sequence, Exception expectedException)
110110
{
111111
return sequence.WithException(actualException => actualException.Should().BeSameAs(expectedException));
112112
}

src/Logging.Assertions/Logging.Assertions.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.Logging.Assertions</PackageProjectUrl>
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
1212
<PackageReleaseNotes>
13+
1.3.0
14+
- Fix an assertion message with the `WithArguments()` method.
15+
- Add the support of the `WithException()` followed by the `WithArguments()` method. (fixes #11).
16+
1317
1.2.0
1418
- Improve the BeginScope() to use a delegate to assert complex state objects.
1519
- Add a BeginScopeAsDictionary() method to check the state as dictionary string/object (useful for Application Insights logs).
16-
20+
1721
1.1.0
1822
- Add the support to assert the log message template and the parameters.
1923

tests/Logging.Assertions.Tests/LoggerMockTest.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ public void LogError_WithException_SameObjectReference()
157157
var logger = new LoggerMock<ObjectToLog>();
158158
logger.SetupSequence()
159159
.LogInformation("Log before error")
160-
.LogError("Log Error")
160+
.LogError("Log Error {Id}")
161161
.WithException(exception)
162+
.WithArguments(1234)
162163
.LogTrace("Log Trace after error")
163164
.LogDebug("Log Debug after error")
164165
.LogInformation("Log Information after error")
@@ -180,8 +181,9 @@ public void LogError_WithException_WithActionDelegate()
180181
var logger = new LoggerMock<ObjectToLog>();
181182
logger.SetupSequence()
182183
.LogInformation("Log before error")
183-
.LogError("Log Error")
184+
.LogError("Log Error {Id}")
184185
.WithException(e => e.Should().BeSameAs(exception))
186+
.WithArguments(1234)
185187
.LogTrace("Log Trace after error")
186188
.LogDebug("Log Debug after error")
187189
.LogInformation("Log Information after error")
@@ -203,8 +205,9 @@ public void LogError_WithException_ExceptionDifferent()
203205
var logger = new LoggerMock<ObjectToLog>();
204206
logger.SetupSequence()
205207
.LogInformation("Log before error")
206-
.LogError("Log Error")
208+
.LogError("Log Error {Id}")
207209
.WithException(new DivideByZeroException("Other exception"))
210+
.WithArguments(1234)
208211
.LogTrace("Log Trace after error")
209212
.LogDebug("Log Debug after error")
210213
.LogInformation("Log Information after error")
@@ -280,6 +283,7 @@ public void LogWithMessageTemplate_DelegateAssertion_WrongExpectedArgumentCount(
280283
.LogInformation("Log information with parameters {Id}, {Name} and {Object}")
281284
.WithArguments(100, args =>
282285
{
286+
args.Should().HaveCount(3);
283287
args["Id"].Should().Be(1234);
284288
args["Name"].Should().Be("The name");
285289
args["Object"].Should().BeEquivalentTo(new { Property = "I am object" });
@@ -290,7 +294,7 @@ public void LogWithMessageTemplate_DelegateAssertion_WrongExpectedArgumentCount(
290294

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

296300
[Fact]
@@ -322,7 +326,7 @@ public void LogWithMessageTemplate_ParametersAssertion_WrongExpectedArgumentCoun
322326

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

328332
[Fact]
@@ -594,7 +598,7 @@ public void Invoke()
594598
public void InvokeWithException(Exception exception)
595599
{
596600
this.logger.LogInformation("Log before error");
597-
this.logger.LogError(exception, "Log Error");
601+
this.logger.LogError(exception, "Log Error {Id}", 1234);
598602
this.logger.LogTrace("Log Trace after error");
599603
this.logger.LogDebug("Log Debug after error");
600604
this.logger.LogInformation("Log Information after error");

0 commit comments

Comments
 (0)