SharePoint Unit Testing

Typemock Isolator makes it easy to test code that is dependent on SharePoint, without using the SharePoint framework, so you can test your code (including WebParts) without changing it.

The problem with unit testing SharePoint
SharePoint is a very popular development platform. But it was not designed with testability in mind. It’s not possible to replace the different objects with hand-rolled mocks, and its “read-only” mentality makes it hard to set up to return the values you need for testing.

In order to test SharePoint code, you need to perform long operations like retracting and deploying just to get ready to test. In this sort of the environment, unit tests’ greatest value – immediate feedback – gets lost, and therefore the developers are discouraged from doing unit testing.

Typemock Isolator's solution
Unit testing SharePoint code requires isolation from the framework. With Typemock Isolator you can write unit tests for your logic without changing it. Isolator helps simulate the SharePoint framework (without even the need to having it installed) so your tests run locally, quickly and give you the confidence your code works correctly...even before it loads into the browser!

Isolator's SharePoint unit test in action
Let’s look at an example. We have a Web Part which contains a label that tells us the number of messages in the SharePoint list “Messages”.  Here’s the class:

 

public class MailboxWebPart : WebPart

    {

        private Label lblNewMessages;

 

        protected void CreateChildControls(int i)

        {

            CreateChildControls();

        }

      

        protected override void CreateChildControls()

        {

            lblNewMessages = new Label();

            lblNewMessages.Text = GetMessageNumberText();

 

            this.Controls.Add(lblNewMessages);

            base.CreateChildControls();

        }

 

        private string GetMessageNumberText()

        {

            // Open the site

            using (SPSite site = new SPSite("http://www.nosuchsite.com"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList messages = web.Lists["Messages"];

                   

                    // Check the message count and return the correct text

                    int numberOfItems = messages.ItemCount;

                    if (numberOfItems == 0)

                    {

                        return "No new messages.";  

                    }

                    else

                    {

                        return "New messages: " + numberOfItems;

                    }

                }

            }

        }

    }

 

Our main logic is in the GetMessageNumberText method. However, there are a number of problems with testing:
•    The method is private.
•    We need a site called “www.nosuchsite.com” just for the test.
•    The SPSite object is created inside the code-under-test and cannot be replaced with another one.
•    For testing the two cases (no messages and new messages) we’ll need to setup the SharePoint list differently.
 

Let’s see how we can easily and quickly test it with Isolator

 

[TestMethod]

public void GetMessageNumberText_ZeroMessages_NoNewMessagesText()

{

    // Fake the site

    SPSite fakeSite = Isolate.Fake.Instance<SPSite>();

    Isolate.Swap.NextInstance<SPSite>().With(fakeSite);

 

    // Simulate Zero messages in the SharePoint database

    Isolate.WhenCalled(() =>

        fakeSite.OpenWeb().Lists["Messages"].ItemCount)

        .WillReturn(0);

 

    // Invoke the private method we want to test

    MailboxWebPart webPart = new MailboxWebPart();

    string result = (string) Isolate.Invoke.Method(webPart, "GetMessageNumberText");

   

    // Chect the returned text

    Assert.AreEqual("No new messages.", result);

   

}

 

Note that we don’t need SharePoint running or even installed: we just need the SharePoint DLLs to compile the tests. We fake the entire SharePoint object set with just a few lines (even the SPSite object), invoke the private method with a short API and test the result.

Download a free trial of Typemock's SharePoint Unit Testing tool to start writing your own SharePoint tests or click through our resources below for all the information and support you need to effectively unit test in SharePoint.


 

Download Typemock Isolator SharePoint Unit Testing Tool!