Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CollectionAssert.Contains against a collection

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?

like image 980
Jon Avatar asked Mar 28 '12 09:03

Jon


3 Answers

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) 
like image 195
Henk Holterman Avatar answered Oct 22 '22 08:10

Henk Holterman


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);
like image 32
Arnaud F. Avatar answered Oct 22 '22 10:10

Arnaud F.


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 ) );
like image 23
onedaywhen Avatar answered Oct 22 '22 10:10

onedaywhen