Reputation: 40062
I want to write something like:
var list = new List<int>(){1,2,3};
var bigList = new List<int>(){1,2,3,4,5,6,7,8,9};
CollectionAssert.Contains(bigList, list);
I can get an error similar to:
Expected: collection containing < 1,2,3 >
But was: < 1,2,3,4,5,6,7,8,9 >
Is it possible to use the contains method against another collection?
Upvotes: 6
Views: 5673
Reputation: 972
The purpose of using CollectionAssert.IsSubsetOf(list, bigList)
is most often that you want to see which of the items from list is not included in the bigList. Testing that all items from list
is contained in bigList
only return true or false, which doesn't give me any helpful debugging information of what is wrong. I want the test to fail and also give the information which of the items was not in bigList
.
So for me, I always convert the statement CollectionAssert.IsSubsetOf(list, bigList)
to
var list = new List<int>(){0,1,2,3,9}; // note the 0 and 9 is added to the list
var bigList = new List<int>(){1,2,3,4,5,6,7,8};
CollectionAssert.IsEmpty(list.Except(bigList));
Which gives the result
Expected <empty>
But was: <0, 9>
In other words, list
contains 0 and 9, which are not contained in bigList
Upvotes: 0
Reputation: 57073
Same functionality, different syntax (NUnit's constraint style, closer to natural language, which is a quality of a good test):
var list = new List<int>(){1,2,3};
var bigList = new List<int>(){1,2,3,4,5,6,7,8,9};
Assert.That( list, Is.SubsetOf( bigList ) );
Upvotes: 1
Reputation: 8462
From MSDN
Use CollectionAssert.IsSubsetOf
:
var list = new List<int>(){1,2,3};
var bigList = new List<int>(){1,2,3,4,5,6,7,8,9};
CollectionAssert.IsSubsetOf(list, bigList);
Upvotes: 2
Reputation: 273711
The signature is
CollectionAssert.Contains (ICollection collection, Object element)
And it checks if element
(singular) is inside collection
.
It is not a method to check for sub-lists.
You should probably use:
CollectionAssert.IsSubsetOf (ICollection subset, ICollection superset)
Upvotes: 7