为了实现这一点(但没有这样做),我正在反思预期和实际对象的属性,并确保它们的值相等。只要它们的属性是单个对象,即不是列表、数组,这将按预期工作IEnumerable
......如果属性是某种列表,则测试失败(在循环Assert.AreEqual(...)
内部)。for
public void WithCorrectModel<TModelType>(TModelType expected, string error = "")
where TModelType : class
{
var actual = _result.ViewData.Model as TModelType;
Assert.IsNotNull(actual, error);
Assert.IsInstanceOfType(actual, typeof(TModelType), error);
foreach (var prop in typeof(TModelType).GetProperties())
{
Assert.AreEqual(prop.GetValue(expected, null), prop.GetValue(actual, null), error);
}
}
如果处理列表属性,如果我改为使用,我会得到预期的结果,CollectionAssert.AreEquivalent(...)
但这需要我强制转换为ICollection
,这反过来又要求我知道列出的类型,我不(想要)。
它还要求我知道哪些属性是列表类型,而我不知道该怎么做。
那么,我应该如何断言任意类型的两个对象是等价的呢?
注意:我特别不想要求它们相等,因为一个来自我的测试对象,另一个是在我的测试类中构建的,可以比较。