chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi there,
I am facing issue with collection objects while writing unit test using typemock.
actually i want to add atleast one item in collection to make it enter into the loops.
It will be great if anyone can help on same.
Below given are the 3 sample which are facing same kind of problem.

sample 1:
//newsList is a splist object
SPListItemCollection collection = newsList.GetItems(queryRegion);

if (collection.Count > 0)
SPListItem item = collection[0];
//do something with item here....




//newsList is a splist object
SPListItemCollection collection = newsList.GetItems(query);
foreach (SPListItem item in collection)
//do something with item here....


//dttable is datatable containing n no of rows

foreach (DataRow drow in dttable.Rows)
//do somthing with drow....

asked by (1.4k points)

1 Answer

0 votes

This should be quit simple using our special API for collections WillReturnCollectionValuesOf :)

In the example bellow once SPList.Items is called it will return a collection with one SPListItem.

SPList fakeList = Isolate.Fake.Instance<SPList>();
Isolate.WhenCalled(() => fakeList.Items).WillReturnCollectionValuesOf( new List<SPListItem>

Please let me know if it helps.
answered by (35.5k points)