单元测试的IList与CollectionAssert单元测试、IList、CollectionAssert

2023-09-04 00:52:12 作者:〆視嬿丶洳掵

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

The MSTest framework has a CollectionAssert that accepts ICollections. My method returns an IList. Apparently a list is not a collection..

有没有办法让我的IList的一个ICollection的?

Are there ways to make my IList an ICollection?

推荐答案

您可以调用的ToArray()扩展方法就可以了 - 阵列实现的ICollection

You could call the ToArray() extension method on it - Array implements ICollection

编辑:另外,虽然名单,其中,T> 实现ICollection的,的IList< T> 只实现的ICollection< T> 不实现ICollection的,所以如果你知道在测试的项目是名单,其中,T> ,你应该能够施展它...

Also, while List<T> implements ICollection, IList<T> only implements ICollection<T> which does not implement ICollection, so if you know the item in the test is a List<T>, you should be able to cast it...