Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSTest - why is there no AreEqual(object, object, IComparer<T>)? [closed]

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?

like image 596
buddybubble Avatar asked Nov 01 '22 12:11

buddybubble


1 Answers

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.

like image 70
Stephen Byrne Avatar answered Nov 08 '22 05:11

Stephen Byrne