November 20

Unit Testing SharePoint with the Microsoft SharePoint Emulator

Last modified by sbrickey on 2/12/2013 3:58 PM
In the SPC Exhibit hall, one of the Microsoft booth stations was focused on the developers. One of their announcements (which was also made at the BUILD conference) is about the SharePoint Emulator shims.

Basically, shims are a way to inject your own implementation of code. This is generally used for unit testing, especially when it comes to static methods and sealed classes. The biggest example involves the DateTime.Now property, since it's a common goal that your unit tests have specific time references. Shims provide the method for controlling the response.

Normally, you'd create a single shim (such as a DateTime shim). But to provide the same level of unit test support within SharePoint, a single shim just won't cut it. Once you start to get into a large number of shims, they can be grouped together and called an emulator.

This is what Microsoft has made available. They wrote shims for a lot of the SharePoint functionality, and packaged them up in the "Microsoft.SharePoint.Emulators" NuGet package.

Using the emulator is straight forward, by simply using the SharePointEmulationScope disposable object to wrap around the code being tested.

using System;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Emulators;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using FluentAssertions;


public void TestMethod1()


    using (new SharePointEmulationScope(Microsoft.QualityTools.Testing.Emulators.EmulationMode.Enabled))


        // Arrange

        var tSite = new SPSite("http://server");

        var tWeb = tSite.RootWeb;

        var tList_Id = tWeb.Lists.Add("sample list", "this is a sample list that only exists in emulation", SPListTemplateType.GenericList);

        var tList = tWeb.Lists[tList_Id];


        // Act

        var tItem = tList.Items.Add();

        tItem["Title"] = "abc";



        // Assert



    } // using emulation

} // void TestMethod1

Note: this test executes in approximately 920ms on a recent workstation... pretty zippy, I must say! :)

Notice the parameter for the SharePointEmulationScope... in the test above, I'm setting the EmulationMode to Enabled... the alternative option (PassThrough) allows the code to execute against the native API... so it's quick and easy to switch between local unit testing and live database integration testing.

I have noticed a few issues:
- I was hoping to show a smaller test... simply changing the SPWeb.Title property... unfortunately, no shim was created for Title. This will be somewhat of a repetitive theme, and will hopefully be addressed as the package is updated.
- Since there is NO dependency on the database, there is NO way to load existing SharePoint structures/content. This means that you can use ANY SPSite URL you like (kinda nice)... it also means that NONE of your existing lists, content types, etc... will be able to be loaded into the emulator... so if you're testing a lists' fields, you'll need to add everything yourself within the unit test.

comments powered by Disqus