Skip to content

Commit ffc2dfa

Browse files
chullybunCopilot
andauthored
v5.11.0 (#103)
* - *Enhancement:* Added `AssertorBase.Logs` to enable access to the logs captured during execution of the test for assertion purposes. This is intended to be used in conjunction with the existing `ExpectLogContains` expectation to enable additional (more advanced) assertions against the logs. * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Eric Sibly [chullybun] <eric@thesiblys.com> --------- Signed-off-by: Eric Sibly [chullybun] <eric@thesiblys.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4b5f3f9 commit ffc2dfa

21 files changed

Lines changed: 87 additions & 41 deletions

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Represents the **NuGet** versions.
44

5+
## v5.11.0
6+
- *Enhancement:* Added `AssertorBase.LogMessages` to enable access to the logs captured during execution of the test for assertion purposes. This is intended to be used in conjunction with the existing `ExpectLogContains` expectation to enable additional (more advanced) assertions against the logs.
7+
58
## v5.10.0
69
- *Enhancement:* `TestSharedState` updated to enable request-based (isolated) state; with the corresponding `GetHttpRequestId()` method now made public.
710
- *Enhancement:* Added `ApiTesterBase.AddPreRunAction()`, `AddPostRunBeforeExpectationsAction()`, `AddPostRunAfterExpectationsAction()` and `AddPostRunAction()` to enable the addition of actions to be executed at different stages of the `Run`/`RunAsync` process for every underlying test. This will enable the addition of common pre/post processing logic without having to explicitly add to each test.

Common.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>5.10.0</Version>
3+
<Version>5.11.0</Version>
44
<LangVersion>preview</LangVersion>
55
<Authors>Avanade</Authors>
66
<Company>Avanade</Company>

src/UnitTestEx.Azure.Functions/Azure/Functions/HttpTriggerTester.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public async Task<ActionResultAssertor> RunAsync(Expression<Func<TFunction, Task
146146

147147
await ExpectationsArranger.AssertAsync(logs, ex).ConfigureAwait(false);
148148

149-
return new ActionResultAssertor(Owner, result, ex);
149+
return new ActionResultAssertor(Owner, result, logs, ex);
150150
}
151151

152152
private void CheckRoute(HttpRequest request, string? route, (string? Name, object? Value)[] @params)

src/UnitTestEx.Azure.Functions/Azure/Functions/ServiceBusTriggerTester.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public async Task<VoidAssertor> RunAsync(Expression<Func<TFunction, Task>> expre
9898

9999
await ExpectationsArranger.AssertAsync(logs, ex).ConfigureAwait(false);
100100

101-
return new VoidAssertor(Owner, ex);
101+
return new VoidAssertor(Owner, logs, ex);
102102
}
103103

104104
/// <summary>

src/UnitTestEx/AspNetCore/HttpTesterBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected async Task<HttpResponseMessageAssertor> SendAsync(HttpMethod httpMetho
100100
Owner.ExecutePostRunBeforeExpectationsActions(this);
101101
await AssertExpectationsAsync(res).ConfigureAwait(false);
102102
Owner.ExecutePostRunAfterExpectationsActions(this);
103-
return new HttpResponseMessageAssertor(Owner, res);
103+
return new HttpResponseMessageAssertor(Owner, LastLogs, res);
104104
}
105105
finally
106106
{
@@ -136,7 +136,7 @@ protected async Task<HttpResponseMessageAssertor> SendAsync(HttpMethod httpMetho
136136
Owner.ExecutePostRunBeforeExpectationsActions(this);
137137
await AssertExpectationsAsync(res).ConfigureAwait(false);
138138
Owner.ExecutePostRunAfterExpectationsActions(this);
139-
return new HttpResponseMessageAssertor(Owner, res);
139+
return new HttpResponseMessageAssertor(Owner, LastLogs, res);
140140
}
141141
finally
142142
{
@@ -171,7 +171,7 @@ protected async Task<HttpResponseMessageAssertor> SendAsync(HttpMethod httpMetho
171171
Owner.ExecutePostRunBeforeExpectationsActions(this);
172172
await AssertExpectationsAsync(res).ConfigureAwait(false);
173173
Owner.ExecutePostRunAfterExpectationsActions(this);
174-
return new HttpResponseMessageAssertor(Owner, res);
174+
return new HttpResponseMessageAssertor(Owner, LastLogs, res);
175175
}
176176
finally
177177
{

src/UnitTestEx/Assertors/ActionResultAssertor.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ namespace UnitTestEx.Assertors
2424
/// </summary>
2525
/// <param name="owner">The owning <see cref="TesterBase"/>.</param>
2626
/// <param name="result">The <see cref="IActionResult"/>.</param>
27+
/// <param name="logs">The logs captured during execution.</param>
2728
/// <param name="exception">The <see cref="Exception"/> (if any).</param>
28-
public class ActionResultAssertor(TesterBase owner, IActionResult result, Exception? exception) : AssertorBase<ActionResultAssertor>(owner, exception)
29+
public class ActionResultAssertor(TesterBase owner, IActionResult result, IEnumerable<string?>? logs, Exception? exception) : AssertorBase<ActionResultAssertor>(owner, logs, exception)
2930
{
3031
/// <summary>
3132
/// Gets the <see cref="IActionResult"/>.
@@ -485,16 +486,17 @@ public ActionResultAssertor AssertJson(string json, params string[] pathsToIgnor
485486
/// </summary>
486487
/// <param name="httpRequest">The optional requesting <see cref="HttpRequest"/> with <see cref="HttpContext"/>; otherwise, will default.</param>
487488
/// <returns>The corresponding <see cref="HttpResponseMessageAssertor"/>.</returns>
488-
public HttpResponseMessageAssertor ToHttpResponseMessageAssertor(HttpRequest? httpRequest = null) => ToHttpResponseMessageAssertor(Owner, Result, httpRequest);
489+
public HttpResponseMessageAssertor ToHttpResponseMessageAssertor(HttpRequest? httpRequest = null) => ToHttpResponseMessageAssertor(Owner, Result, LogMessages, httpRequest);
489490

490491
/// <summary>
491492
/// Converts the <see cref="ValueAssertor{TValue}"/> to an <see cref="HttpResponseMessageAssertor"/>.
492493
/// </summary>
493494
/// <param name="owner">The owning <see cref="TesterBase"/>.</param>
494495
/// <param name="result">The <see cref="IActionResult"/> to convert.</param>
496+
/// <param name="logs">The log messages captured during execution.</param>
495497
/// <param name="httpRequest">The optional requesting <see cref="HttpRequest"/>; otherwise, will default.</param>
496498
/// <returns>The corresponding <see cref="HttpResponseMessageAssertor"/>.</returns>
497-
internal static HttpResponseMessageAssertor ToHttpResponseMessageAssertor(TesterBase owner, IActionResult result, HttpRequest? httpRequest)
499+
internal static HttpResponseMessageAssertor ToHttpResponseMessageAssertor(TesterBase owner, IActionResult result, IEnumerable<string?>? logs, HttpRequest? httpRequest)
498500
{
499501
var sw = Stopwatch.StartNew();
500502
using var ms = new MemoryStream();
@@ -517,7 +519,7 @@ internal static HttpResponseMessageAssertor ToHttpResponseMessageAssertor(Tester
517519
sw.Stop();
518520
owner.LogHttpResponseMessage(hr, sw);
519521

520-
return new HttpResponseMessageAssertor(owner, hr);
522+
return new HttpResponseMessageAssertor(owner, logs, hr);
521523
}
522524
}
523525
}

src/UnitTestEx/Assertors/AssertorBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ namespace UnitTestEx.Assertors
1111
/// Represents the base test assert helper.
1212
/// </summary>
1313
/// <param name="owner">The owning <see cref="TesterBase"/>.</param>
14+
/// <param name="logs">The logs captured during execution.</param>
1415
/// <param name="exception">The <see cref="Exception"/> (if any).</param>
15-
public abstract class AssertorBase(TesterBase owner, Exception? exception)
16+
public abstract class AssertorBase(TesterBase owner, IEnumerable<string?>? logs, Exception? exception)
1617
{
1718
private static List<Func<AssertorBase, ApiError[], bool>>? _assertErrorsExtentions;
1819

@@ -28,6 +29,11 @@ public abstract class AssertorBase(TesterBase owner, Exception? exception)
2829
/// </summary>
2930
public TesterBase Owner { get; } = owner ?? throw new ArgumentNullException(nameof(owner));
3031

32+
/// <summary>
33+
/// Gets the log messages captured during execution.
34+
/// </summary>
35+
public IEnumerable<string?> LogMessages { get; } = logs ?? [];
36+
3137
/// <summary>
3238
/// Gets the <see cref="System.Exception"/>.
3339
/// </summary>
@@ -58,7 +64,7 @@ internal void AssertErrorsUsingExtensions(ApiError[] errors)
5864
return;
5965
}
6066

61-
if (!Assertor.TryAreErrorsMatched(errors, Array.Empty<ApiError>(), out var errorMessage))
67+
if (!Assertor.TryAreErrorsMatched(errors, [], out var errorMessage))
6268
Implementor.AssertFail(errorMessage);
6369
}
6470
}

src/UnitTestEx/Assertors/AssertorBaseT.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ namespace UnitTestEx.Assertors
1010
/// Represents the base test assert helper that distinguishes between <see cref="AssertException"/> and <see cref="AssertSuccess"/>.
1111
/// </summary>
1212
/// <param name="owner">The owning <see cref="TesterBase"/>.</param>
13+
/// <param name="logs">The logs captured during execution.</param>
1314
/// <param name="exception">The <see cref="Exception"/> (if any).</param>
14-
public abstract class AssertorBase<TSelf>(TesterBase owner, Exception? exception) : AssertorBase(owner, exception) where TSelf : AssertorBase<TSelf>
15+
public abstract class AssertorBase<TSelf>(TesterBase owner, IEnumerable<string?>? logs, Exception? exception) : AssertorBase(owner, logs, exception) where TSelf : AssertorBase<TSelf>
1516
{
1617
/// <summary>
1718
/// Asserts that an <see cref="Exception"/> was thrown during execution.

src/UnitTestEx/Assertors/HttpResponseMessageAssertor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Microsoft.Net.Http.Headers;
44
using System;
5+
using System.Collections.Generic;
56
using System.Linq;
67
using System.Net.Http;
78
using System.Net.Mime;
@@ -13,8 +14,9 @@ namespace UnitTestEx.Assertors
1314
/// Represents the <see cref="HttpResponseMessage"/> test assert helper.
1415
/// </summary>
1516
/// <param name="owner">The owning <see cref="TesterBase"/>.</param>
17+
/// <param name="logs">The log messages captured during execution.</param>
1618
/// <param name="response">The <see cref="HttpResponseMessage"/>.</param>
17-
public class HttpResponseMessageAssertor(TesterBase owner, HttpResponseMessage response) : HttpResponseMessageAssertorBase<HttpResponseMessageAssertor>(owner, response)
19+
public class HttpResponseMessageAssertor(TesterBase owner, IEnumerable<string?>? logs, HttpResponseMessage response) : HttpResponseMessageAssertorBase<HttpResponseMessageAssertor>(owner, logs, response)
1820
{
1921
/// <summary>
2022
/// Asserts the the <see cref="HttpResponseMessageAssertorBase.Response"/> <see cref="HttpResponseMessage.Headers"/> <see cref="HeaderNames.Location"/> matches the <paramref name="expectedUri"/>.

src/UnitTestEx/Assertors/HttpResponseMessageAssertorBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/UnitTestEx
22

33
using System;
4+
using System.Collections.Generic;
45
using System.Net.Http;
56
using System.Net.Mime;
67
using UnitTestEx.Abstractions;
@@ -15,14 +16,20 @@ namespace UnitTestEx.Assertors
1516
/// Initializes a new instance of the <see cref="HttpResponseMessageAssertorBase{TSelf}"/> class.
1617
/// </remarks>
1718
/// <param name="owner">The owning <see cref="TesterBase"/>.</param>
19+
/// <param name="logs">The log messages captured during execution.</param>
1820
/// <param name="response">The <see cref="HttpResponseMessage"/>.</param>
19-
public abstract class HttpResponseMessageAssertorBase(TesterBase owner, HttpResponseMessage response)
21+
public abstract class HttpResponseMessageAssertorBase(TesterBase owner, IEnumerable<string?>? logs, HttpResponseMessage response)
2022
{
2123
/// <summary>
2224
/// Gets the owning <see cref="TesterBase"/>.
2325
/// </summary>
2426
public TesterBase Owner { get; } = owner ?? throw new ArgumentNullException(nameof(owner));
2527

28+
/// <summary>
29+
/// Gets the log messages captured during execution.
30+
/// </summary>
31+
public IEnumerable<string?> LogMessages { get; } = logs ?? [];
32+
2633
/// <summary>
2734
/// Gets the <see cref="HttpResponseMessage"/>.
2835
/// </summary>

0 commit comments

Comments
 (0)