1

In FluentAssertions I can use the AllProperties.But(obj => obj.property_I_do_not_want) to remove specific properties from a comparison assertion which is fine when I know the names of the properties I want to ignore but in my situation I only want to ignore unitialized properties. For now I would be willing to just ignore one's equal to null but if there's a solution that also excludes primitives set to their default values that would be extra handy.

I started out be trying to write an extension method for the PropertyAssertions class but can't figure out how to create an IEnumerable<Expression<T>> that includes an Expression<T> for accessing each property to ignore to pass to the But method.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
carlin.scott
  • 6,214
  • 3
  • 30
  • 35

1 Answers1

3

If you're willing to try the 2.0 beta, you can use a lambda in the new ShouldBeEquivalentTo() API to exclude certain properties like this:

subject.ShouldBeEquivalentTo(expected, options =>
    options.Excluding(ctx => ctx.PropertyPath == "Level.Level.Text"));

If you want, you can even encapsulate this in a custom rule (a class implementing ISelectionRule) like this:

subject.ShouldBeEquivalentTo(expected, options => 
    options.Using(new ExcludeUninitializedProperties()));

In fact, you could even make this the default for particular types by overriding the factory method that creates the initial options:

EquivalencyAssertionOptions<TSubject>>.Default = () =>    
    EquivalencyAssertionOptions<TSubject>>.Default.Using(new ExcludeUninitializedProperties())
Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • This answer was a huge help but I need further assistance. I was already using 2.0 beta for MBUnit support but there doesn't seem to be a "Using" method for the Default property as mentioned in your last code snippet. Unfortunately I need to override the default because what I left out in my question was the fact that I'm running the comparison between two lists using ShouldContainInOrder() which doesn't take EquivalencyAssertionOptions. – carlin.scott Sep 04 '12 at 17:57
  • It's in the FluentAssertions.Equivalency namespace. But it wouldn't work with ShouldContainInorder() extension method, since that is not based on the same engine. – Dennis Doomen Sep 05 '12 at 07:32
  • 1
    Hi @DennisDoomen, does ExcludeUninitializedProperties still exist in version 3.2.1.0 can´t seem to find it. – Ahmed Alejo Dec 29 '14 at 12:51
  • Hi @AdeAlejo. It was just an example of creating a custom `ISelectionRule` and using it in an assertion. – Dennis Doomen Dec 30 '14 at 13:04
  • Hi @DennisDoomen, ok, i thought it was from an old version of FluentAssertions due to the post´s age. But you agree that this isn´t possible given there isn´t access to the actual object being asserted on, via the **ISelectionRule** – Ahmed Alejo Dec 30 '14 at 17:33
  • 1
    Yes, you have found a gap in my theory ;-). Instead, you could implement `IAssertionRule` and simply return `true` if the involved property is not initialized. That should prevent any additional assertions from getting invoked. – Dennis Doomen Dec 30 '14 at 20:24