Unit-testing IList with CollectionAssert
Asked Answered
N

2

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?

Nicety answered 19/3, 2009 at 14:39 Comment(1)
The connect issue for this. Usual Microsoft response, tsss. connect.microsoft.com/VisualStudio/feedback/details/477870/…Lineolate
A
13

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

Autarch answered 19/3, 2009 at 14:43 Comment(1)
I think IEnumerable for the parameters would have been a better choice. NUnit has got this one right.Levity
L
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.)

Laundes answered 20/4, 2009 at 18:27 Comment(4)
and how exactly is this related to the question?Nicety
I send in two list<string> (IList) that are compared in CollectionAssert is that your problem...Laundes
I'm sorry, I didn't read in enough on your answer. Please refer @Lee's answer about why this is working but not a solution to my problem.Nicety
I edited your answer to remove a bit of noise so the actual answer is clearer. If you mind, please feel free to roll back.Nicety

© 2022 - 2024 — McMap. All rights reserved.