I am trying to compare 2 complex objects with AutoFixture's OfLikeness
but unfortunately without success. While comparing nested objects( also with OfLikeness
) works as expected, comparing a master object results with error saying that child objects don't match. I assume that the problem is that Likeness apply semantic comparrer only to a master object and nested objects are compared using default Equal implementation which checks for reference match(May be my assumption is wrong?).
This could clarify what I am trying to achieve:
public class ComplexMasterObject{
public ChildFirst child1 {get;set;}
public ChildSecond child2 {get;set;}
public string SimpleProp {get;set;}
}
public class ChildFirst {
public string SomeStringProp1 {get;set;}
public int SomeIntProp1 {get;set;}
}
public class ChildSecond {
public string SomeStringProp1 {get;set;}
public int SomeIntProp1 {get;set;}
}
Test:
public void TestLikeness_Success()
{
var expected = new ComplexMasterObject {
Child1 = new ChildFirst {
SomeStringProp1 = "ChildFirst SomeStringProp1",
SomeIntProp1 = 1
},
Child2 = new ChildSecond {
SomeStringProp1 = "ChildSecond SomeStringProp1",
SomeIntProp1 = 2
},
SimpleProp = "ComplexMasterObject SimpleProp"
};
var input = new ComplexMasterObject {
Child1 = new ChildFirst {
SomeStringProp1 = "ChildFirst SomeStringProp1",
SomeIntProp1 = 1
},
Child2 = new ChildSecond {
SomeStringProp1 = "ChildSecond SomeStringProp1",
SomeIntProp1 = 2
},
SimpleProp = "ComplexMasterObject SimpleProp"
};
var child1Likeness = expected.Child1.AsSource().OfLikeness<ChildFirst>();
var child2Likeness = expected.Child2.AsSource().OfLikeness<ChildSecond>();
var masterLikeness = expected.AsSource().OfLikeness<ComplexMasterObject>();
child1Likeness.ShouldEqual(input.Child1); //Passes
child2Likeness.ShouldEqual(input.Child2); //Passes
masterLikeness.ShouldEqual(input);
// The provided value `ComplexMasterObject` did not match the expected value `ComplexMasterObject`. The following members did not match:
- Child1.
- Child2.
}
When I serialize expected
and input
objects to JSON and compare the results, they are identical.
I tried to use Resemblance, But it didn't work either:
var proxy = expected.AsSource().OfLikeness<ComplexMasterObject>().CreateProxy();
proxy.Equals(input); // returns false
How can perform a semantic comparison on complex object including nested complex properties?
Thank you in advance.
Equals
implementation for all matched members. – Jessy