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 everybody,
I'm trying to mock a very simple method in SharePoint, but I cannot find the problem... please help:

My method:

        public String TestSite(String SiteUrl)
            //Try to count the number of Document Libraries in the SPWeb
            String strReturn = String.Empty;
            SPSite mySite = null;
            SPWeb myWeb = null;

                mySite = new SPSite(SiteUrl);
                myWeb = mySite.OpenWeb();
                SPListCollection myListCol = myWeb.Lists;

                int LibraryCounter = 0;
                foreach (SPList myList in myListCol)
                    if (myList.BaseType == SPBaseType.DocumentLibrary)

                strReturn = LibraryCounter.ToString();
            catch (System.Exception ex)
                strReturn = ex.ToString();
                if (myWeb != null) myWeb.Dispose();
                if (mySite != null) mySite.Dispose();

            return strReturn;

My test method:

        public void TestSiteTest()
            using (RecordExpectations recorder = RecorderManager.StartRecording())
                Guid Guid0 = Guid.NewGuid();
                Guid Guid1 = Guid.NewGuid();

                SPSite mySiteMocked = new SPSite("");  //Mock the Site

                SPWeb myWebMocked = RecorderManager.CreateMockedObject<SPWeb>();  //Mock the Web
                recorder.ExpectAndReturn(mySiteMocked.OpenWeb(), myWebMocked);

                SPListCollection myListCollMocked = 
                       RecorderManager.CreateMockedObject<SPListCollection>();  //Mock the Coll.
                recorder.ExpectAndReturn(myWebMocked.Lists, myListCollMocked);
                //recorder.ExpectAndReturn(myListCollMocked.Count, 2);
                SPList myListMocked0 = 
                        RecorderManager.CreateMockedObject<SPList>();  //Mock List one
                recorder.ExpectAndReturn(myListCollMocked[0], myListMocked0);
                recorder.ExpectAndReturn(myListMocked0.ID, Guid0);
                recorder.ExpectAndReturn(myListMocked0.BaseType, SPBaseType.DocumentLibrary);

                SPList myListMocked1 = 
                         RecorderManager.CreateMockedObject<SPList>();  //Mock List two
                recorder.ExpectAndReturn(myListCollMocked[1], myListMocked1);
                recorder.ExpectAndReturn(myListMocked1.ID, Guid1);
                recorder.ExpectAndReturn(myListMocked1.BaseType, SPBaseType.GenericList);

            MyObjets target = new MyObjets();
            string SiteUrl = string.Empty;
            string expected = "1";
            string actual;
            actual = target.TestSite(SiteUrl);
            Assert.AreEqual(expected, actual);

If the code arrives in the loop ?foreach? of my method, it gives a ?Object reference not set to an instance of an object.? error. If I stop the run just before the loop, I can see in the Immediate window that I have two Lists as I have mocked them (BaseType DocumentLibrary and GenericList).
Any idea will be very welcome, because I'm not able to see the problem...
Honda Bros 400 specifications
asked by gavd (600 points)

1 Answer

0 votes

First, I want to apologize we didn't get to you sooner.

You indeed uncovered a bug :oops:. What happens is that when constructing the mock SPListCollection, something happens that causes the foreach (or GetEnumerator which is actually called) to return null.

What I did instead to pass the test (almost, except the verification exception because ID was not called) was to replace the foreach loop with this code:
SPList myList = myListCol[0];
    if (myList.BaseType == SPBaseType.DocumentLibrary)
myList = myListCol[1];
    if (myList.BaseType == SPBaseType.DocumentLibrary)

That's right - I broke the foreach into separate calls. Not a nice workaround, but still a workaround.

I'll add this bug to our task list, and give you an update when we fix it.

Note that you can click the following link to learn more on using sharepoint!

answered by gilz (14.5k points)