-2

MSTest provides CollectionAssert class and based on this question I am comparing a returned Dictionary matches my expectations: .NET Dictionaries have same keys and values, but aren't "equal"

However then I deliberately populated my Dictionary in a different order to the returned version but with the exact same elements... now CollectionAssert.AreEqual fails when I call:

CollectionAssert.AreEqual((ICollection)expected, (ICollection)ret)

This doesn't seem a very good equality test - do I need to roll my own or does MSTest provide something out of the box?

Community
  • 1
  • 1
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

2 Answers2

2

You can use AreEquivalent:

CollectionAssert.AreEquivalent((ICollection)expected, (ICollection)ret)

This method test if the collections have the same elements in the same quantity, but in any order.

Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
2

In the same class there is another method called AreEquivalent();

This will return you a true if the two ICollection objects you send as parameter have the same elements with the same amount. In this case the order does not matter. This is the biggest difference between AreEqual() and AreEquivalent().

Below you can find the official explanation from MSDN page.

"Verifies that the specified collections are equivalent. Two collections are equivalent if they have the same elements in the same quantity, but in any order. Elements are equal if their values are equal, not if they refer to the same object."

And your call to the function would be same as you did for AreEqual(), see below;

CollectionAssert.AreEquivalent((ICollection)expected, (ICollection)ret)
Cengiz Araz
  • 680
  • 9
  • 17