1

Closed

Exclude properties on nested objects

description

We'd like to compare two aggregate roots ignoring a few audit properties (CreatedBy, CreatedOn) on all entities within the root.

AllPropertiesBut() only excludes properties on the root object.

I think this is similar to this post below in that we'd like to configure how nested objects are compared:

http://fluentassertions.codeplex.com/discussions/284312
Closed Aug 25, 2012 at 2:19 PM by
All done with the public beta of Fluent Assertions 2.0

comments

dennisdoomen wrote Mar 9, 2012 at 12:30 PM

brianlow wrote Apr 24, 2012 at 5:31 AM

Great to see! Let me know if I can help. Here is an example use case for this feature. Hopefully it fits within your vision:


public class EntityBase
{
public Guid Id { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedOn { get; set; }
}

public class Invoice : EntityBase
{
public DateTime InvocingDate { get; set; }
public List Details { get; set; }
}

public class Detail
{
public Product Product { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal Total { get; set;
}

public class Product : EntityBase
{
public string Name { get; set; }
}

[Test]
public void ShouldDoSomething()
{
var expected = CreateExpectedOrder();
var actual = GetOrderFromDatabase();

actual.Id.Should().Be(expected.Id);
// SQL Server truncates datetimes to nearest 20ms so we've added our own date assert
actual.InvocingDate.Should().BeCloseTo(expected.InvoiceingDate);
// don't compare actual.ModifiedBy and ModifiedOn
actual.Details.Should.Have.SameCountAs(expected.Details);
for (int i = 0; i<actual.Details.Count(); i++)
{
actual.Details[i].Quantity.Should().Be(expected.Details[i].Quantity);
actual.Details[i].UnitPrioce.Should().Be(expected.Details[i].UnitPrice);
actual.Details[i].Total.Should().Be(expected.Details[i].Total);
actual.Details[i].Product.Id.Should().Be(expected.Details[i].Product.Id);
actual.Details[i].Product.Name.Should().Be(expected.Details[i].Product.Name);
// don't compare actual.Details[i].Product.ModifiedBy and ModifiedOn
}
// yuck, we have similar repetitive code for all our entities. can we replace with FluentAssertions?
}

mopdam wrote Apr 25, 2012 at 8:02 AM

I added a new issue for supporting 'Should().BeCloseTo()' on DateTime values.
See http://fluentassertions.codeplex.com/workitem/12070

dennisdoomen wrote May 2, 2012 at 6:59 AM

I've started a StackOverflow discussion to get some feedback and/or ideas for a better (and fluent) syntax.
http://stackoverflow.com/questions/10402287/feedback-request-alternative-syntax-for-comparing-properties-in-fluent-assertio

dennisdoomen wrote May 4, 2012 at 10:49 AM

Well, the StackOverflow discussion was closed unexpectingly. But I like Brain Low's approach.

dennisdoomen wrote May 25, 2012 at 11:17 AM

Still working on it. It involves big changes under the hood.

wrote May 28, 2012 at 6:39 PM

Resolved with changeset 78100.

brianlow wrote May 29, 2012 at 2:21 AM

Great! I'll take a look later this week.

dennisdoomen wrote May 30, 2012 at 5:09 PM

** Closed by dennisdoomen 5/28/2012 11:39 AM

dennisdoomen wrote May 30, 2012 at 5:09 PM

Wasn't finished at all

wrote May 30, 2012 at 5:10 PM

Resolved with changeset 78299.

dennisdoomen wrote May 31, 2012 at 5:39 AM

** Closed by dennisdoomen 5/30/2012 10:10 AM

dennisdoomen wrote May 31, 2012 at 5:39 AM

I did it again....:-(

wrote Jun 2, 2012 at 7:50 PM

Associated with changeset 78461: Added compatibility with WinRT RC (Visual Studio 2012 RC)

wrote Jun 3, 2012 at 6:36 PM

Associated with changeset 78485: Exclusion and inclusion of properties didn't always work because PropertyInfo objects cannot be compared.

wrote Jun 5, 2012 at 6:56 PM

Associated with changeset 78551: Removed some duplication between WinRT and .NET extensions

brianlow wrote Jun 11, 2012 at 8:48 PM

Here is a self-contained sample with tests for our use case:

https://gist.github.com/2912543

Let me know if you'd like any help testing.

dennisdoomen wrote Jun 12, 2012 at 4:15 AM

Thanks Brian. I'm still working on this, but my recently born son is still keeping me from getting enough sleep. Should get better soon though.

brianlow wrote Jun 12, 2012 at 4:50 AM

Congratulations!

wrote Jun 15, 2012 at 8:17 PM

Associated with changeset 79068: Moved the ObjectExtensions.FindProperty() method to TypeExtensions.

wrote Jun 15, 2012 at 8:31 PM

Associated with changeset 79069: Documented the IMatchingRule interface and made it clear that implementers will never receive nulls

wrote Jun 17, 2012 at 8:45 AM

Associated with changeset 79081: Added a bit more documentation

wrote Jun 21, 2012 at 9:52 AM

Associated with changeset 79202: Properly documented ISelectionRule and its conrete implementations

wrote Jun 26, 2012 at 8:16 PM

Associated with changeset 79579: Introduced an IAssertionRule that takes the subject and expectation value and the PropertyInfo of the subject property. Returns true if the rule handled the assertion and no other rules should be considered.

wrote Jul 4, 2012 at 8:44 PM

Associated with changeset 79855: Introduced an IStructuralEqualityStep named ApplyAssertionRulesEqualityStep that executes the assertion rules according to the config.
Applied some minor refactoring to simplify some of the internal workings of structural validation.

wrote Jul 5, 2012 at 12:07 PM

Associated with changeset 79902: Replaced StringEqualityStep with a generic implementation of the new IAssertionRule

wrote Jul 6, 2012 at 6:08 PM

Associated with changeset 79994: Encapsulated more logic in the StructuralEqualityContext.
Simplified the AssertionRule so that casting is no longer necessary.
Added additional type checks to the AssertionRule.

wrote Jul 6, 2012 at 6:32 PM

Resolved with changeset 79995: Replaced the DateTimeEqualityStep with a more generic AssertionRule

wrote Jul 8, 2012 at 12:24 PM

Resolved with changeset 80037: Introduced the new extension methods ShouldBeStructurallyEqualTo() and ShouldAllBeStructurallyEqualTo() that perform a recursive comparison.
Rewrote the existing PropertyAssertionSpecs using this new API.

dennisdoomen wrote Jul 9, 2012 at 7:11 PM

** Closed by dennisdoomen 7/8/2012 5:24 AM

dennisdoomen wrote Jul 9, 2012 at 7:11 PM

Argh

wrote Jul 9, 2012 at 7:16 PM

Associated with changeset 80113: Added a spec to verify that exclusing a property on a collection item supports lambda expressions.

wrote Jul 11, 2012 at 9:50 AM

Associated with changeset 80175: Added support for excluding properties in a structural comparison using a nested property expression

wrote Jul 11, 2012 at 12:43 PM

Associated with changeset 80183: Renamed IgnorePropertySelectionRule to ExcludePropertyByPathSelectionRule.
Renamed ComparisonConfiguration to StructuralEqualityConfiguration.

wrote Jul 11, 2012 at 1:20 PM

Associated with changeset 80188: Refactored ExpressionExtensions.GetPropertyPath()

wrote Jul 13, 2012 at 8:04 PM

Associated with changeset 80277: Reordered some of the structural equality specs.

wrote Jul 21, 2012 at 8:56 AM

Associated with changeset 80487: Finished support for overridding an assertion with an action and based on a predicate.

wrote Jul 23, 2012 at 7:01 PM

Associated with changeset 80521: Changed the override method to be more fluent.
Added the option to override the assertions for all properties of a certain type.
Properly documented the other members of the StructuralEqualityConfiguration.
Fixed a compile error in the .NET for Metro specs.

wrote Aug 1, 2012 at 7:44 PM

Resolved with changeset 80739: Introduced a mechanism so that the failure message can specify the {context} tag.
Ensured the collection assertions are context aware.

dennisdoomen wrote Aug 3, 2012 at 8:16 PM

** Closed by dennisdoomen 8/1/2012 12:44 PM

dennisdoomen wrote Aug 3, 2012 at 8:16 PM

32424

wrote Aug 3, 2012 at 8:16 PM

Associated with changeset 80811: Added extra specs to verify that custom rules are properly evaluated

wrote Aug 6, 2012 at 7:26 PM

Associated with changeset 80866: Improved the debugging experience of the structural comparison configuration.

wrote Aug 7, 2012 at 6:28 PM

Associated with changeset 80885: Included the current equality configuration in the assertion exception.

dennisdoomen wrote Aug 7, 2012 at 6:29 PM

Now it's done.....really done!

wrote Aug 16, 2012 at 1:20 PM

Associated with changeset 81039: Fixed bug in AttributeBasedFormatter