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

Feature request: Assert.Equivalent overloads for filtering #3058

Open
weitzhandler opened this issue Nov 14, 2024 · 4 comments
Open

Feature request: Assert.Equivalent overloads for filtering #3058

weitzhandler opened this issue Nov 14, 2024 · 4 comments

Comments

@weitzhandler
Copy link

Assert.IsEquivalent is a powerful method to compare complex objects. But sometimes, a single property in the graph is to be ignored.
In those cases an alternative method has to be used or those fields must be manually cleared from the desired operand.

An easy solution for that could be adding overloads to Assert.IsEquivalent that:

  • takes a params string[] excludedPropertyNames (or include to include only those)
  • takes a params Expression<Func<T, object?>> excludedPropertiesSelector (like in EF primary key setup)
  • can take a Comparer<T> lambda and individually compare each object. This should be used for each element in collection when comparing two collections
@bradwilson
Copy link
Member

The intended usage of Assert.Equivalent is that you only put the things you care about in the expected value, and everything else is ignored. The easiest way to do this is not to use a concrete type for the expected value, but instead to use an anonymous value with just the property values you care about.

For example, if you have this class:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

and you want to only validate the name without the age, you would do this:

var person = GetAPersonFromSomewhere();

Assert.Equivalent(new { FirstName = "Brad", LastName = "Wilson" }, person);

@bradwilson
Copy link
Member

The other way you could handle this, if you want to use concrete types on both sides, is use Assert.Equal with your own comparison function:

var expected = new Person { FirstName = "Brad", LastName = "Wilson" };
var actual = new Person { FirstName = "Brad", LastName = "Wilson", Age = 123 };

Assert.Equal(
    expected,
    actual,
    (person1, person2) =>
        person1.FirstName == person2.FirstName &&
        person1.LastName == person2.LastName
);

@weitzhandler
Copy link
Author

Hi Brad and thanks for taking the time to read my post and write your replies!

In my scenario I'm comparing a complex object graph containing properties I need to exclude from comparison. Creating an anonymous object is a bit of an overkill.

Fluent Assertions does offer this functionality (see Selecting Members), alas it's not used in the project I'm working on.

@bradwilson
Copy link
Member

We obviously don't have a fluent syntax from which to derive such exclusions, so any exclusion API would be something like a string based system such as TopLevel.SubLevel1.SubLevel2.PropertyName.

I'll leave this open to see how much uptake it gets from the community.

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

No branches or pull requests

2 participants