Sharepoint unittesting with typemock

We would like to test all our code by means of Unittests, now when using sharepoint this is difficult because code has to be executed against a live Sharepoint server with your latest site definition rolled out. This is not just impractical but also very slow, especially the fist time.

A colleague came with a solution, why don’t you try mocking. After some googling i came across the following article: First steps with TypeMock and the Sharepoint API. After reading this i decided to download TypeMock and try it out. TypeMock turns out to be a very powerfull mocking framework (to some too powerful) which allows us to mock the sharepoint framework very easy.

An example of a unittest with some code which gets a relative URL from a picture from a picturelibrary. The code:

///

/// Return the url of the Picture from a PictureLibrary by ID
///

///Id of the picture/// Picture url
public static string GetPicturLinkById(int PictureId, SPWeb currentWeb)
{
   string PictureLink = string.Empty;
   //Open the picture library
   SPPictureLibrary Picturelibrary = (SPPictureLibrary)currentWeb.Lists[Resources.ProjectResources.PictureListName];
    try
    {
       //Get the SPListitem with id PictureId
       SPListItem PictureListItem = Picturelibrary.GetItemById(PictureId);
       //Get the ServerRelativeUrl
       PictureLink = PictureListItem.File.ServerRelativeUrl;
    }
   catch (Exception ex)
    {    //If the item doesn’t exist in the coll. return empty string
         if (ex is ArgumentException)
         {
               return string.Empty;
         }
         else
         {
             throw new Exception(“Error while retrieving a picture url”, ex);
         }
     }
     //return the url
     return PictureLink;
    }

The unittest code

[Test]
public void GetFotoLinkByIdTest()
{
    const string FILENAME = “/test/file1.hpg”;

     Mock mockSPweb = MockManager.MockObject(Constructor.Mocked);
    Mock spListCollection = MockManager.MockObject(Constructor.Mocked);
    Mock spPictureLibrary = MockManager.MockObject(Constructor.Mocked);
   Mock listItem = MockManager.MockObject(Constructor.Mocked);
   Mock file = MockManager.MockObject(Constructor.Mocked);
   //Expect get of relativeurl, fill the call with FILENAME
   file.ExpectGetAlways(“ServerRelativeUrl”, FILENAME);
   //Expect get of FILE, fill this with out SPFILE mock file
   listItem.ExpectGetAlways(“File”, file.Object);
   //Expect a call to GetItemById, return our SPListItem mock file
   spPictureLibrary.ExpectAndReturn(“GetItemById”, listItem.Object);
   //Expect a get at an idex which returns our SPPictureLibrary mock listItem
   spListCollection.ExpectGetIndex(spPictureLibrary.Object);
   //Expect a get of the attribute lists of SPWeb, return our mocked SPListCollection
   mockSPweb.ExpectGetAlways(“Lists”, spListCollection.Object);

   //Instantiate SPWeb rootWeb with our mocked object
   SPWeb rootWeb = mockSPweb.MockedInstance;
   //Check wheter filename is returned from the function
   Assert.AreEqual(FILENAME, Recept.GetFotoLinkById(1, rootWeb));
   //Verify of all code is called
   MockManager.Verify();
}

Typemocks reflecting allow you to start testing your code without rewriting all your existing code to Interfaces. You can even test static methods and step into your code while unittesting! Typemock is really a recommendation when you want to unittest your sharepoint Code.