To accomplish this (but failing to do so) I'm reflecting over properties of an expected and actual object and making sure their values are equal. This works as expected as long as their properties are single objects, i.e. not lists, arrays, IEnumerable
... If the property is a list of some sort, the test fails (on the Assert.AreEqual(...)
inside the for
loop).
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);
}
}
If dealing with a list property, I would get the expected results if I instead used CollectionAssert.AreEquivalent(...)
but that requires me to cast to ICollection
, which in turn requries me to know the type listed, which I don't (want to).
It also requires me to know which properties are list types, which I don't know how to.
So, how should I assert that two objects of an arbitrary type are equivalent?
Note: I specifically don't want to require them to be equal, since one comes from my tested object and one is built in my test class to have something to compare with.