-
-
Notifications
You must be signed in to change notification settings - Fork 540
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
Show the values of properties that are collections when comparing equivalence of collections of anonymous objects #2140
Comments
This is a limitation of the formatting code, not the specific implementation of |
Thanks for the pointer @dennisdoomen. I (think I) have tracked it down to the The problem comes down to the fact that anonymous types do have a custom It looks like one way to use FluentAssertion's own ability to write the member values would be to detect if the value being formatted is an anonymous object, (in addition to the existing check for a custom public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
{
...
if (HasDefaultToStringImplementation(value) || IsAnonymousType(value))
{
WriteTypeAndMemberValues(value, formattedGraph, formatChild);
}
else
...
}
private static bool IsAnonymousType(Type type)
{
return type.Namespace is null
&& Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), inherit: false)
&& type.Name.Contains("AnonymousType", StringComparison.Ordinal)
&& (type.Name.StartsWith("<>", StringComparison.Ordinal)
|| type.Name.StartsWith("VB$", StringComparison.Ordinal));
} This is the first time I've opened the source of FluentAssertions (which is very easy to navigate and a joy to work with - I wish other codebases I work on were so great!), you may know of a better way to implement this. |
Yes, that looks like the right direction to me as well. I guess the same applies to records, since by default, we treat them the same as struct.
Thank you for those kind words. Our goal is to make the code look like we want our real projects to look like too. |
Description
When asserting equivalency of a collection of anonymous objects (using
ContainsEquivalentOf
orBeEquivalentTo
) that contain properties which are themselves collections, the failure message does not report the contents of those properties.The first test failure message (of the reproducible example below) shows the values of the collection in the property of both the expected and actual objects.
The only difference in the second test is that the objects are anonymous. Here the failure message does not show the values of the collections, instead it shows the collections' types.
I would be great if FluentAssertions could show the collections' values for anonymous objects the same way it does for named typed objects.
Reproduction Steps
Expected behavior
ContainEquivalentOf
should produce a message containing the values of the collectionExpected actual {{ Collection = {"alpha"} }} to contain equivalent of { Collection = {"beta"} }.
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
net6.0
FluentAssertions v6.10.0
Other information
No response
The text was updated successfully, but these errors were encountered: