-
Notifications
You must be signed in to change notification settings - Fork 723
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
Value equality on subset of object properties #4687
Comments
This is a neat idea, so I'm going to label it Perhaps some variation involving: Assert.That(thing, Is.EqualTo(new {
SomeInt = 5,
SomeDouble = 5d,
SomeString = "bla bla don't care",
}).UsingPropertiesComparer()); |
Ooh that's a cool idea. I assumed some |
@Rabadash8820 Usually, when I have to do assertions like this, then I use an assertion framework - e.g. FluentAssertions. Using FluentAssertions can specify it like this orderDto.Should().BeEquivalentTo(order, options => options
.Including(o => o.OrderNumber)
.Including(pi => pi.Name == "Date")); Or perhaps using |
It would be pretty handy if there was a way to do value equality on a subset of an object's properties. For example, suppose I have the following class definition:
and a method that returns a
Thing
. Let's say that the method sets non-default values for all the object's properties, but I only care about the values of the return object's numeric properties (SomeInt
andSomeDouble
). My options are to:Do individual equality assertions on those properties, which doesn't scale very cleanly for real-world objects with many properties.
Define an "expected" object and use equality assertion with
UsingPropertiesComparer
, which requires me to include those other non-default values in my expected object, even though my test really only cares about the two numeric properties (also doesn't scale well for objects with many properties).It would be great if NUnit had a version of
UsingPropertiesComparer
that only compared a subset ofThing
's properties. Perhaps using anExpression
or collection ofExpression
s for consumers to specify the properties that they care about. Something like the following would be awesome, as the test code would not have to change as more properties are added toThing
(obviously with better naming 😅).The text was updated successfully, but these errors were encountered: