question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AssertionScope with a custom strategy does not report each assertion

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dennisdoomencommented, Aug 28, 2019

Yep. Just released it.

1reaction
eNeRGy164commented, Aug 28, 2019

i think 8 minutes ago 😛

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found