Project Description
The SPTypeMock Wrapper project encapsulates the functionality provided in TypeMock into a convenient set of wrapper classes to facility test unit in SharePoint.
El proyecto SPTypeMock Wrapper provee las clases envoltorio necesarias para programar Pruebas Unitarias para SharePoint utilizando el FrameWork de TypeMock

To be able to create Unit Test for SharePoint, it is necessary the use of a Mocking FrameWork. Because multiple SharePoint classes are sealed or don’t have public constructors, the only Mocking FrameWork that can be used at the moment is TypeMock version 5.0 ( The SPTypeMock wrapper classes help to create the mock objects needed in the test classes.

Para poder crear Pruebas Unitarias para SharePoint es necesario utilizar un FrameWork de Mocking. SharePoint utiliza múltiples clases que son selladas y/o que no contienen constructores públicos; TypeMock versión 5 (urlhttp:\\ es el único FrameWork de Mocking en la actualidad que puede soslayar este problema. Las clases envoltorio de SPTypeMock ayudan a la creación de los objetos mock necesarios para crear las clases de prueba.


Project initiated by Carlos Segura (MVP MOSS) ( and Gustavo Velez (MVP MOSS) (, but everybody is welcome to participate and make the wrappers more complete and efficient.

Proyecto iniciado por Carlos Segura (MVP MOSS) ( y Gustavo Velez (MVP MOSS) (, pero todas las personas interesadas son bienvenidas para participar y hacer las clases más completas y eficientes


1 - Mocking a List Collection
-- Method to be tested:

        public static string TestMock_02()
            string strReturn = String.Empty;

                using (SPSite mySite = new SPSite("http://MiServidor"))
                    using (SPWeb myWeb = mySite.OpenWeb())
                        int intTeller = 0;
                        foreach (SPList oneList in myWeb.Lists)

                        strReturn = intTeller.ToString();
            catch (Exception ex)
                strReturn = ex.ToString();

            return strReturn;

-- Mocking method:

        public void TestMethod2()
            MockSPSite mockSite = new MockSPSite("TestSite");          // 1
            MockSPWeb mockWeb = new MockSPWeb("TestWeb");  // 2

            MockSPList mockList0 = new MockSPList("MyList0");         // 3
            MockSPList mockList1 = new MockSPList("MyList1");      
            MockSPList mockList2 = new MockSPList("MyList2");
            mockWeb.Lists = new MockSPListCollection(new[]             // 4

            mockSite.Mock.ExpectGetAlways("RootWeb", mockWeb.GetInstance());  //5

            SPWeb WebMocked = mockWeb.GetInstance();    // 6

            using (RecordExpectations recorder = RecorderManager.StartRecording())    // 7
                SPSite SiteMocked = new SPSite("");    // 8
                recorder.ExpectAndReturn(SiteMocked.OpenWeb(), WebMocked);    // 9

            string expected = "3";    // 10
            string actual;
            actual = Program.TestMock_02();
            Assert.AreEqual(expected, actual);

- First zone: lines 1/7: here are the mocked objects created using the wrapper classes of SPTypeMock. Lines 1 and 2 create SPSite and SPWeb mocked objects. The lines below 3 make three mocked Lists and in line 4 are the Lists added to the List Collection of the SPWeb mocked object. Line 5 add the SPWeb object as the root web of the mocked SPSite object, and, finally, line 6 creates a new SPWeb object based in the mocked SPWeb object. This is necessary, because the code in the second zone doesn’t allow to use mocked objects directly

- Second zone: lines 7 and 9. Around the “recorder” statement are the objects that will be used in the class to be tested. At line 8 is a SPSite object created that will replace the real SPSite object in the class to be tested; line 9 assigns the value of the mocked SPWeb object with the mocked Lists to the SPSite object

- Third zone: from line 10. This is the normal Unit Test code where the expectations and actual return values are set up and evaluated

**Known issues**

- The mocked objects in the test class and the “real” objects in the class to be tested are created in two separated threads. If the objects are disposed in the tested class (“using” statement or “Dispose()” method of SPSIte or SPWeb), SharePoint will log a error “ERROR: request not found in the TrackedRequests. We might be creating and closing webs on different threads”

**Work to be done**

The wrapper classes are not complete... they need to be expanded and extended...

Last edited Sep 21, 2008 at 9:34 PM by csegura, version 11