使用CollectionAssert对IList进行单元测试

Bor*_*ens 15 .net ilist unit-testing mstest icollection

MSTest框架有一个CollectionAssert,可以接受ICollections.我的方法返回一个IList.显然列表不是集合..

有没有办法让我的IList成为ICollection?

Lee*_*Lee 12

您可以在其上调用ToArray()扩展方法 - Array实现ICollection

编辑:另外,虽然List<T>实现ICollection,IList<T>只实现ICollection<T>没有实现ICollection,所以如果你知道测试中的项是a List<T>,你应该能够投射它...