我试图将2个复杂对象与AutoFixture的OfLikeness
进行比较,但遗憾的是没有成功。虽然比较嵌套对象(也与OfLikeness
)按预期工作,但比较主对象结果与错误说明子对象不匹配。我假设问题是Likeness仅将语义比较器应用于主对象,并使用默认的Equal实现来比较嵌套对象,该实现检查引用匹配(可能是我的假设是错误的?)。
这可以澄清我想要实现的目标:
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;}
}
测试:
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.
}
当我将expected
和input
个对象序列化为JSON并比较结果时,它们是相同的。
我尝试使用相似性,但它也没有用:
var proxy = expected.AsSource().OfLikeness<ComplexMasterObject>().CreateProxy();
proxy.Equals(input); // returns false
如何对复杂对象(包括嵌套复杂属性)进行语义比较?
提前谢谢。
答案 0 :(得分:4)
C#中的复杂结构对象相等(带有类)大多数时候都很麻烦,因为默认情况下类使用引用相等。
虽然可以使用AutoFixture附带的SemanticComparison包来执行复杂的结构对象相等(但是作为单独的NuGet包分发)。
示例: SemanticComparer<T>
[Fact]
public void TestComplexClassEquality()
{
// Arrange
var value = new ComplexMasterObject
{
Child1 = new ChildFirst
{
SomeStringProp1 = "1",
SomeIntProp1 = 2
},
Child2 = new ChildSecond
{
SomeStringProp1 = "3",
SomeIntProp1 = 4
},
SimpleProp = "5"
};
var other = new ComplexMasterObject
{
Child1 = new ChildFirst
{
SomeStringProp1 = "1",
SomeIntProp1 = 2
},
Child2 = new ChildSecond
{
SomeStringProp1 = "3",
SomeIntProp1 = 4
},
SimpleProp = "5"
};
var sut =
new SemanticComparer<ComplexMasterObject>(
new MemberComparer(
new AnyObjectComparer()),
new MemberComparer(
new ChildFirstComparer()),
new MemberComparer(
new ChildSecondComparer()));
// Act
var actual = sut.Equals(value, other);
// Assert
Assert.True(actual);
}
示例: Likeness<T>
及其Resemblance
代理
[Fact]
public void TestComplexClassEqualityResemblance()
{
// Arrange
var value = new ComplexMasterObject
{
Child1 = new ChildFirst
{
SomeStringProp1 = "1",
SomeIntProp1 = 2
},
Child2 = new ChildSecond
{
SomeStringProp1 = "3",
SomeIntProp1 = 4
},
SimpleProp = "5"
};
var other = new ComplexMasterObject
{
Child1 = new ChildFirst
{
SomeStringProp1 = "1",
SomeIntProp1 = 2
},
Child2 = new ChildSecond
{
SomeStringProp1 = "3",
SomeIntProp1 = 4
},
SimpleProp = "5"
};
var likeness =
new Likeness<ComplexMasterObject>(
value,
new SemanticComparer<ComplexMasterObject>(
new MemberComparer(
new AnyObjectComparer()),
new MemberComparer(
new ChildFirstComparer()),
new MemberComparer(
new ChildSecondComparer())));
var sut = likeness.ToResemblance();
// Act
var actual = sut.Equals(other);
// Assert
Assert.True(actual);
}
对象定义
public sealed class AnyObjectComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
return object.Equals(x, y);
}
public int GetHashCode(object obj)
{
return obj.GetHashCode();
}
}
public sealed class ChildFirstComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
var value = x as ChildFirst;
var other = y as ChildFirst;
if (value == null || other == null)
return false;
return value.SomeIntProp1 == other.SomeIntProp1
&& value.SomeStringProp1 == other.SomeStringProp1;
}
public int GetHashCode(object obj)
{
return obj.GetHashCode();
}
}
public sealed class ChildSecondComparer : IEqualityComparer
{
public new bool Equals(object x, object y)
{
var value = x as ChildSecond;
var other = y as ChildSecond;
if (value == null || other == null)
return false;
return value.SomeIntProp1 == other.SomeIntProp1
&& value.SomeStringProp1 == other.SomeStringProp1;
}
public int GetHashCode(object obj)
{
return obj.GetHashCode();
}
}