Posts Tagged ‘C#’

MsTest: CollectionAssert и HashSet

Печально, но дотнетовский System.Collections.Generic.HashSet<T> не имплементит System.Collections.ICollection. Из-за этого МС-овский же Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent() безбожно не компилирутеся.

var hashSet = new HashSet();
CollectionAssert.AreEquivalent(new string[]{}, hashSet);

Error 125 The best overloaded method match for 'Microsoft.VisualStudio.TestTools.UnitTesting.CollectionAssert.AreEquivalent(System.Collections.ICollection, System.Collections.ICollection)' has some invalid arguments.

Вот такая хрень компилируется, но, что неудивительно, падает при попытке привести хэшсет к ICollection. Это ещё печальнее:

var hashSet = new HashSet();
CollectionAssert.AreEquivalent(new string[]{}, (ICollection)hashSet);

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.HashSet`1[System.String]' to type 'System.Collections.ICollection'.
Вот такая ерунда работает ок, но выглядит как костыль (собственно, им и является):

var hashSet = new HashSet();
CollectionAssert.AreEquivalent(new string[]{}, new List<string>(hashSet));

Приходится писать свой хелпер.

Мир несовершенен, друзья. Любите его, чините его, и он будет лучше.

теплый пол.