16

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?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Boris Callens
  • 90,659
  • 85
  • 207
  • 305
  • 2
    The connect issue for this. Usual Microsoft response, tsss. https://connect.microsoft.com/VisualStudio/feedback/details/477870/collectionassert-cannot-be-used-with-hashset-t-and-possibly-other-collections# – Rudi Jun 08 '12 at 12:28

2 Answers2

12

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

Lee
  • 1,125
  • 6
  • 7
  • 1
    I think IEnumerable for the parameters would have been a better choice. NUnit has got this one right. – Gishu Nov 09 '11 at 06:16
1

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.)

Boris Callens
  • 90,659
  • 85
  • 207
  • 305
salgo60
  • 957
  • 5
  • 16