Unit-testing IList with CollectionAssert

2019-02-21 08:29发布

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

Are there ways to make my IList an ICollection?

2条回答
forever°为你锁心
2楼-- · 2019-02-21 08:58

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

Edit: 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...

查看更多
太酷不给撩
3楼-- · 2019-02-21 09:08

You can send in a List

    List<string> actual = new List<string>(){"1","2","3"};
    List<string> expected = new List<string>(){"1","2","**EditCaseFalse**"};
    CollectionAssert.AreEqual(actual,expected)

I get back Failed (third element does not match.)

查看更多
登录 后发表回答