-
Notifications
You must be signed in to change notification settings - Fork 783
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
Comments
The intended usage of 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); |
The other way you could handle this, if you want to use concrete types on both sides, is use 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
); |
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. |
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 I'll leave this open to see how much uptake it gets from the community. |
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:params string[] excludedPropertyNames
(or include to include only those)params Expression<Func<T, object?>> excludedPropertiesSelector
(like in EF primary key setup)Comparer<T>
lambda and individually compare each object. This should be used for each element in collection when comparing two collectionsThe text was updated successfully, but these errors were encountered: