Pex and Moles Testing Framework for SharePoint

Here is a quickstart for working with unit testing framework for SharePoint. This Framework is called Moles. You can download it here. It is free academic use and Microsoft partners.

So how this Framework works: It allows you to replace any method with by you defined delegate. So here is a simplistic function:

Simple Intro

public static bool IsInFuture(DateTime date)
	return date > DateTime.Now;

So this function is not easily unit testable, because the DateTime.Now is not a constant value. What we can do with moles is following:

public static void IsInFutureTest()
      MDateTime.NowGet = () => new DateTime(2010,09,01);
      bool actual = SuperClass.IsInFuture(new DateTime(2010, 09,03));

The interesting thing here is the MDateTime construct, that comes from moles and the function replaces the actual method call with by us defined function that returns a constant date.
So here is what we have to do when we want to use the MDateTime construct:

  1. Install Pex and Moles.
  2. Add a new item of type “Moles and Stubs for Testing”  into the unit test project.  This file has to be called mscorlib.moles, because  the DateTime class is located in the mscorlib.dll. When you add this file the moles Framework analyses the dll and create the stubs. These stubs are the classes with the “M” prefix.
  3. Add following attribute [HostType(“Moles”)] to the test method. This tells moles framework to replace the actual methods with custom delegates.
  4. For this type you have to also add following row to the assemblyinfo.cs of the test project [assembly: Microsoft.Moles.Framework.MoledType(typeof(DateTime))]

SharePoint Unit Testing

You can stub in this way all SharePoint objects e.g. SPContext, SPList etc. All you have to do is to add new moles file with the name Microsoft.SharePoint.moles. This has actually one drawback and it is that it is a lot of coding to fullfill all the expectations that you use in the method to be tested. There come behaviors for SharePoint in play. It offers stubs with predefined behaviors against you can unit test your code. In all these cases you  don’t access the physically SharePoint, in extreme there can be no SharePoint installed at all.

So where can you find Behaviors for SharePoint. They are part of the Moles project and you can find it on the machine where moles are installed on the following path \Program Files (x86)\Microsoft Moles\Documentation\\behaviors\Microsoft.SharePoint.Behaviors. All You have to do is to compile this project and you have an assembly called Microsoft.SharePoint.Behaviors.dll that you can reference in your test projects. Here is a short sample how you can work with SharePoint behaviors:

public static void  IsListNameCorrectTest()
BSPSite site = new BSPSite();
BSPWeb web = site.SetRootWeb();
BSPList list = web.Lists.SetOne();
list.Title = "TestList";
bool actual = MyClass.IsListNameCorrect(list);

public static class MyClass
public bool IsListNameCorrect(SPList list)
return list.Title.Length < 200;

Tags: , ,

2 Responses to “Pex and Moles Testing Framework for SharePoint”

  1. Dave Graham Says:

    Thanks so much for this quick start. I wanted to get going quickly with some SharePoint testing but did not want to spend forever learing the technology. Your guide had just enough to get me going.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: