AssertionScope with a custom strategy does not report each assertion
See original GitHub issueDescription
Hi,
I am trying to use the new custom strategy logic (introduced on PR1904) so I can take a screenshot on test failure however when I have implemented this it is only asserting on the first assert and not reporting any additional asserts.
Complete minimal example reproducing the issue
Here is a custom strategy which is just a copy of CollectingAssertionStrategy to demonstrate the issue.
public class CustomAssertionStrategy : IAssertionStrategy
{
private readonly List<string> failureMessages = new List<string>();
/// <summary>
/// Returns the messages for the assertion failures that happened until now.
/// </summary>
public IEnumerable<string> FailureMessages => failureMessages;
/// <summary>
/// Discards and returns the failure messages that happened up to now.
/// </summary>
public IEnumerable<string> DiscardFailures()
{
var discardedFailures = failureMessages.ToArray();
failureMessages.Clear();
return discardedFailures;
}
/// <summary>
/// Will throw a combined exception for any failures have been collected since <see cref="StartCollecting"/> was called.
/// </summary>
public void ThrowIfAny(IDictionary<string, object> context)
{
if (failureMessages.Any())
{
var builder = new StringBuilder();
builder.AppendLine(string.Join(Environment.NewLine, failureMessages));
if (context.Any())
{
foreach (KeyValuePair<string, object> pair in context)
{
builder.AppendFormat("\nWith {0}:\n{1}", pair.Key, pair.Value);
}
}
Services.ThrowException(builder.ToString());
}
}
/// <summary>
/// Instructs the strategy to handle a assertion failure.
/// </summary>
public void HandleFailure(string message)
{
failureMessages.Add(message);
}
}
and then
using (var scope = new AssertionScope(new CustomAssertionStrategy()))
{
false.Should().BeTrue();
true.Should().BeFalse();
}
Expected behavior:
Both assertion failures should be reported when using a custom strategy
Actual behavior:
Only the first assertion failure is reported
Versions
5.8.0, .Net Framework 4.7.1
Additional Information
See https://github.com/tomaustin700/FluentAssertionIssue for a full solution demonstrating this issue.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Collect assertion failures using Fluent assertions in C# - ...
My goal is to use fluent assertions to collect all assertion failures and report them. Currently, I use private static void AssertValue(object?
Read more >Using Assertion Scopes to execute multiple Assertions in ...
Using Assertion Scopes, we can batch multiple assertions into a AssertionScope so that FluentAssertions will only throw the one exception at ...
Read more >Master Xunit Testing with fluent assertions and C ... - YouTube
This is a one lecture complete course in fluent assertions used in ... Custom Assertions with Fluent Assertions 0:28:32 Test the Custom ......
Read more >Supercharging .NET unit tests with Fluent Assertions - YouTube
So you've decided your next project is going to use Test-Driven Development (TDD). You've picked out a test framework and created the ...
Read more >AssertJ - fluent assertions java library
Use code completion. Type assertThat followed by the object under test and a dot … and any Java IDE code completion will show...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep. Just released it.
i think 8 minutes ago 😛