Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested AssertionScopes do not print inner scope reportables #2044

Merged
merged 10 commits into from Dec 17, 2022
10 changes: 10 additions & 0 deletions Src/FluentAssertions/Execution/AssertionScope.cs
Expand Up @@ -395,6 +395,16 @@ public void Dispose()
parent.assertionStrategy.HandleFailure(failureMessage);
}

IDictionary<string, object> reportables = contextData.GetReportable();

if (reportables.Count > 0)
{
foreach (KeyValuePair<string, object> reportable in reportables)
{
parent.AddReportable(reportable.Key, reportable.Value.ToString());
}
}
jnyrup marked this conversation as resolved.
Show resolved Hide resolved

parent.AppendTracing(tracing.ToString());

parent = null;
Expand Down
18 changes: 18 additions & 0 deletions Tests/FluentAssertions.Specs/Execution/AssertionScopeSpecs.cs
Expand Up @@ -235,6 +235,24 @@ public void When_using_a_custom_strategy_it_should_include_failure_messages_of_a
.WithMessage("*but found false*but found true*");
}

[Fact]
public void When_nested_scope_is_disposed_it_passes_reports_to_parent_scope()
{
// Arrange/Act
using (var outerScope = new AssertionScope())
{
outerScope.AddReportable("outerReportable", "foo");

using (var innerScope = new AssertionScope())
{
innerScope.AddReportable("innerReportable", "bar");
}

// Assert
outerScope.Get<string>("innerReportable").Should().Be("bar");
}
}

public class CustomAssertionStrategy : IAssertionStrategy
{
private readonly List<string> failureMessages = new();
Expand Down