Skip to content

AssertionScope with a custom strategy does not report each assertion #1118

@tomaustin700

Description

@tomaustin700

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions