Set global ComparingByMembers/ComparingByValues #1757
-
Hi! How can I globally setup the strategy to using ComparingByMembers for all reference types (ignore condition type.OverridesEquals())? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 10 replies
-
Did you try |
Beta Was this translation helpful? Give feedback.
-
That's what's going to happen. Every type, both yours and the .NET types, will be compared by their members. But I'm pretty sure that's not what you intended. However, I'm not clear what you are trying to accomplish. |
Beta Was this translation helpful? Give feedback.
-
It seems you're mixing up how FA compares two instances and how it renders the failure message. If your type, |
Beta Was this translation helpful? Give feedback.
-
Hi @dennisdoomen. I think ran into the same need to apply I incidentally tried I would certainly agree that I only care for the types defined in my application to get this ComparyingByMembers behavior, so I am more inclined for having a way to specify that overriding equals should not determine that a type should be compared by value. I also think it would be convinient if this was something that can be configured on a specific assertion or in a specific assertion scope rather than globally. I even tried code like the one below, but it didn't help: public static EquivalencyAssertionOptions<TExpectation> ExcludingByMembers<TExpectation>(this EquivalencyAssertionOptions<TExpectation> options, params string[] exclusions)
{
using (var scope = new AssertionScope())
{
scope.FormattingOptions.MaxDepth = 100;
return options
.ComparingByMembers<object>()
.AllowingInfiniteRecursion()
.IgnoringCyclicReferences()
.Excluding(memberInfo => exclusions.Any(e => memberInfo.Path.Equals(e, StringComparison.OrdinalIgnoreCase)));
}
} At this point, I keep looking for a workaround that will allow me to upgrade my project to the latest version of FA. Perhaps I need to try harder with the documentation 😄 cc @jnyrup. |
Beta Was this translation helpful? Give feedback.
It seems you're mixing up how FA compares two instances and how it renders the failure message. If your type,
TestClass
in this case, implements value equality (usingEquals
,IEquatable
, etc), then we assume you want to compare two instances using those equality members. We will also useToString
to render the object in the failure message. You can customize that though. Check out the documentation on this here.