Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollectionAssert.AreEquivalent with Custom IEqualityComparer

I have two lists, I want to check whether the two lists are the same ( order not important), and whether it's the same depends on the IEqualityComparer instance I implement.

The ideal case is that I can use CollectionAssert.AreEquivalent with Custom IEqualityComparer. However it seems that CollectionAssert.AreEquivalent doesn't take in any IEqualityComparer.

Any idea on how to do this in a succinct and reusable manner?

like image 958
Graviton Avatar asked Jul 25 '10 04:07

Graviton


1 Answers

CollectionAssert.AreEquivalent is implemented as:

Assert.That(actual, new CollectionEquivalentConstraint(expected), message, args); 

You can write out your assert that way and supply a custom IEqualityComparer with Using:

Assert.That(actual,     new CollectionEquivalentConstraint(expected).Using(customComparer)); 

You can also shorten new CollectionEquivalentConstraint to Is.EquivalentTo:

Assert.That(actual, Is.EquivalentTo(expected).Using(customComparer)); 
like image 165
Quartermeister Avatar answered Sep 24 '22 23:09

Quartermeister