Unit Testing SharePoint Solutions is easy

Unit testing the SharePoint API is impossible, difficult, easy

History:

  • Before 2010 it was almost not possible.
  • In 2010 it was possible by using a 3th party tool or Microsoft Research Pex & Moles (complex)
  • In 2011-2012 the Pex and Moles team did a great job providing examples, white papers and behaviors to help SharePoint developers to unit test their code.
  • Fakes are released with Visual Studio 2012
  • SharePoint Emulators are released available !!!!!!!

Microsoft Research Pex & Moles – Moles examples

SPContext.Current.Web.Url

Uri url = new Uri(http://sp2010/test);
MSPContext.CurrentGet = () => new MSPContext() { 
    WebGet = () => new MSPWeb() {
        UrlGet = () => url.ToString()
    }  
};

SPDocumentLibrary

BSPSite site = new BSPSite();
site.Url = defaultviewUrl;
BSPWeb web = site.SetRootWeb();
MSPDocumentLibrary doclib = new MSPDocumentLibrary();
SPList splist = (SPDocumentLibrary)doclib;
web.Lists.SetOne(splist);
BSPList list = new BSPList(splist);
list.Views.DefaultViewIndex = 0;
BSPView view = list.Views.SetOne();
view.Url = site.Url;
MSPList mlist = new MSPList(splist);
mlist.ParentWebGet = () => web;

SPSecurity.RunWithElevatedPrivileges

MSPSecurity.RunWithElevatedPrivilegesSPSecurityCodeToRunElevated = f => f();

Cookies

MHttpContext.CurrentGet = () => new MHttpContext()
{
    RequestGet = () => new MHttpRequest()
    {
        CookiesGet = () => new MHttpCookieCollection()
        {
            ItemGetString = (s) => new MHttpCookie()
            {
                ValueGet = () => cookievalue
            }
        }
    },
    DisableCustomHttpEncoderGet = () => false
};

SharePoint Emulators

http://blogs.msdn.com/b/visualstudioalm/archive/2012/11/26/introducing-sharepoint-emulators.aspx

VN:F [1.9.20_1166]
Rating: 7.5/10 (4 votes cast)
VN:F [1.9.20_1166]
Rating: +1 (from 1 vote)
Unit Testing SharePoint Solutions is easy, 7.5 out of 10 based on 4 ratings

One thought on “Unit Testing SharePoint Solutions is easy

  1. It’s nearly impossible to find knowledgeable people on this subject, however, you sound like you know what you’re talking about! Thanks