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

}

and

sample2:

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


also


//dttable is datatable containing n no of rows

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

}
asked by teenasharma (1.4k points)

1 Answer

0 votes
Hi,

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>
                                                            {
                                                               Isolate.Fake.Instance<SPListItem>()
                                                            });



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