I'm testing complicated object graphs with FluentAssertions and I want to set defaults for ShouldBeEquivalentTo. The problem is that the object graph re-used the same base-type for a lot of the properties. I just want to set up my AssertionOptionss once per Type but I can't seem to find out how.
Example: I have this object graph:
public class Inner
{
public string One { get; set; }
public string Two { get; set; }
}
public class Outer : Inner
{
public Inner Inner { get; set; }
}
I set up a Custom Extension:
public AndConstraint<InnerAssertions<T>> BeEquivalentTo(T expectation)
{
return base.BeEquivalentTo(expectation,
c => c
.RespectingRuntimeTypes()
.Excluding(r => r.Two));
}
Now I want this test to pass:
new Outer
{
One = "1",
Two = "2",
Inner = new Inner {One = "inner.1", Two = "inner.2"}
}.Should()
.BeEquivalentTo(new Outer
{
One = "1",
Two = "3",
Inner =new Inner {One = "inner.1", Two = "inner.3"}
});
This obviously fails on root.Inner.Two, but I hope this makes clear what I want to do.
One way to achieve this is by using an
IMemberSelectionRuleto excludeInner.Twofrom equivalency comparisonsand then add it to the stat
AssertionOptionswith e.g. aModuleInitializer.