Skip to content

[Feature request] EquivalentTo that compares similar objects of different types #4244

Description

@killergege

Hello,

I'd like to have an overload on EquivalentTo (or a new constraint) that takes an object and will compare it with another object using reflection, which allows you to compare 2 similar objects of different types.
The constraint would check each public property of the target and source objects, using reflection, and will try to find the same property on the other object.
If they are of the same type, or a native type, it uses Equals, if the types are different, it also applies the EquivalentTo constraint.
If the property is a collection, it would apply EquivalentTo to each value of the collection, keeping the order.
The constraint fails if the property don't exist on the other object, or if the Equals returns false.

It would be even better if we could add options (like IgnoreCase for strings) to ignore some specified properties.

We need it a lot as we have entities for each layer of the application, but the objects are often very similar (if not identical) and it is a pain to maintain a comparison of each field, and for each sub-object.

Today most of the answers point to FluentAssertions, which provide BeEquivalentTo() but we don't like the fluent syntax for assertions and prefer NUnit's contraint syntax, and we feel that it does pretty much the same thing as NUnit constraint system so it would be redundant.
So we would reference that library only for that feature and would use the fluent syntax only for that instruction. Not great.

Is it something that has already been discussed (couldn't find anything), and would already have been rejected maybe ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions