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
Hello

I am developing web part for MOSS application in asp.net (c#) 2.0,
I am trying to Mock the SharePoint Objects, but i am getting error on SPListCollection object.

Method to be tested is
public void PopulateDropDown(SPListCollection listCollection, DropDownList ddlListNames)
        {
            foreach (SPList list in listCollection)
            {
                if (list != null)
                {
                    ListItem listItem = new ListItem(list.Title, list.Title);
                    ddlListNames.Items.Add(listItem);
                }
            }
        }


Tet Method written as follow

       [Test]
        [VerifyMocks]

        public void PopulateListNameDropDown_TestWithMock()
        {
            DropDownList ddlListName = new DropDownList();
            SPWeb web = CreateMockedSPWeb();
            SPListCollection listCollection = web.Lists;
            retrieveList.PopulateDropDown(listCollection, ddlListName);
        }

        private SPWeb CreateMockedSPWeb()
        {
            SPWeb web = RecorderManager.CreateMockedObject<SPWeb>();
            SPContext context = RecorderManager.CreateMockedObject<SPContext>();
            SPListCollection listCollection = RecorderManager.CreateMockedObject<SPListCollection>();
            SPList list = RecorderManager.CreateMockedObject<SPList>();

            using (RecordExpectations recorder = RecorderManager.StartRecording())
            {
                recorder.ExpectAndReturn(context.Web, web);
                recorder.ExpectAndReturn(web.Lists, listCollection);
                recorder.ExpectAndReturn(listCollection.Count, 1);
                recorder.ExpectAndReturn(listCollection[0], list).RepeatAlways();
                recorder.ExpectAndReturn(list.Title, "AssetUpload").RepeatAlways();
            }
            return web;
        }


I am getting following error

at Microsoft.SharePoint.SPListCollection.EnsureListsData(Guid webId, String strListName)
at Microsoft.SharePoint.SPListCollection.EnsureListsData(String strListName)
at Microsoft.SharePoint.SPListCollection.Undirty()
at Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator()
at InteractivePortal.WebParts.AJAXEnabledListRetrieve.PopulateDropDown(SPListCollection listCollection, DropDownList ddlList)
at InteractivePortal.WebParts.NUnitTest.TestAJAXEnabledListRetrieve.PopulateListNameDropDown_TestWithMock() in C:MOSSAvanadeSiteInteractivePortalWebParts.NUnitTestTestAJAXEnabledListRetrieve.cs:line 79
at TypeMock.VerifyMocksAttribute.Execute()
at TypeMock.DecoratorAttribute.CallDecoratedMethod()
at TypeMock.ClearMocksAttribute.Execute()
at TypeMock.MethodDecorator.e()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at InteractivePortal.WebParts.NUnitTest.TestAJAXEnabledListRetrieve.PopulateListNameDropDown_TestWithMock() in C:MOSSAvanadeSiteInteractivePortalWebParts.NUnitTestTestAJAXEnabledListRetrieve.cs:line 75








Can you please help me to trac where i am wrong.
asked by dattanavatre (640 points)

4 Answers

0 votes
Hi,

You too have stumbled on a problem we have with foreach, and collections in general. You can see my explanation here.

You may want to look at our new API Sharepoint example, which let's you write less expectation for Sharepoint tests here.
answered by gilz (14.5k points)
0 votes
This problem of SPListCollection and foreach is resolved in new API?
answered by dattanavatre (640 points)
0 votes
Actually no,

The bug is something older and exists on all API's flavors.
We are working on fixing this and will let you know as soon as it is resolved.
answered by lior (13.2k points)
0 votes
I had this problem and eventually came up with this workaround. Worked for SPListItemCollection also.

Hope it helps.
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
    recorder.ExpectAndReturn(context.Web, web);
    recorder.ExpectAndReturn(web.Lists, listCollection);
    var enumerable = new[] {list};
    recorder.ExpectAndReturn(((IEnumerable) listCollection).GetEnumerator(), enumerable.GetEnumerator());
    recorder.ExpectAndReturn(listCollection.Count, 1);
    recorder.ExpectAndReturn(listCollection[0], list).RepeatAlways();
    recorder.ExpectAndReturn(list.Title, "AssetUpload").RepeatAlways();
}
answered by John.Sutcliffe (140 points)
...