سؤال

Writing UnitTests with MSTest I want to assert the equality of a return value vs. the one I'm expecting.

Expected type is a custom type that does not implement the IComparable interface nor the IEquatable interface, thats why I want to give Assert.AreEqual a possibility to compare the two objects.

I am aware that this possibility exists in CollectionAssert.AreEqual. This method however requires two ojects that inherit ICollection which my objects do not.

Why does Assert.AreEqual not allow me to specify a custom comparer? Am I missing something?

هل كانت مفيدة؟

المحلول

Not sure if this is the actual reason, but what if your custom IComparer was faulty - your unit test would be meaningless (bearing in mind that the test framework has no way to tell if you wrote unit tests for it let alone if they are "correct")

Could you just create a comparer in your test?

var com = new MyComparer<Foo>();
int expected=0;
int actual = com.Compare(a,b);
if (actual!=0)
{
  Assert.Fail("oops");
}

Maybe not ideal, but should work... I also found this question from a few years ago on msdn, with no answers - but an interesting approach to the workaround by the question poster.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top