Unit Testing with SharePoint (Part Two)

The following post is from The SharePoint Baker and is reprinted with permission.We previously wrote about his first impressions unit testing with SharePoint.

As you may have read in Part 1, I recently started playing with TypeMock Isolator for SharePoint and trying to introduce some unit testing. I’ve discovered a lot over the last week and so I wanted to put together another post picking up from where I left off last time.

In the last post I began showing how TypeMock could be used to create various “fake” SharePoint objects that could use to test against. I was giving the example of a very simple class that would retrieve a list of documents from a document library, along with some of its properties. The test that had setup expected a single document to be returned and that is what it did. The next step I’ve taken is to use the same classes and tests but to extend so that the test script can cater for several documents being returned- which is probably what the MyDocuments class will be dealing with in the real world.

Returning multiple results

For this example, I started by taking a copy of the existing test script but extended it by adding an additional item, as below:

   1:  var fakeItem2 = fakeList.AddItem();
   2:  fakeItem2["Title"] = "document 2";
   3:  fakeItem2["Created"] = DateTime.Now;
   4:  fakeItem2["Modified"] = DateTime.Now;
   5:  fakeItem2["Created By"] = "User 2";
   6:  fakeItem2["Modified By"] = "User 2";

I also modified the WillReturnCollectionValuesOf property to this:

   1:  Isolate.WhenCalled(() => fakeList.GetItems(fakeQuery)).WillReturnCollectionValuesOf(new[] { fakeItem1, fakeItem2 }); 

Using this approach it turns out has some interesting results. Firstly you get the correct number of items returned – my Assert statement expected 2 results and that’s what it got. However, when I drilled down into those results I found that both results were fakeItem2 – the same list item was being return multiple times! I think what’s happened is that this code fakes the call to GetItems but then fakeItem1/fakeItem2 aren’t properly faked and are being passed into the foreach loop –  if anyone knows exactly what’s wrong I’d be interested to hear. Good news is that I did a bit of research and found a different approach to returning data – one that appears to work for what I want!

This new approach doesn’t add items to the list (using the AddItem method) but instead uses the WhenCalled method of TypeMock to return the different items that we want – probably easier to take a look:

   1:  Isolate.WhenCalled(() => fakeResults[0]["Title"]).WillReturn("document 1");
   2:  Isolate.WhenCalled(() => fakeResults[0]["Created"]).WillReturn(DateTime.Now.AddDays(-2));
   3:  Isolate.WhenCalled(() => fakeResults[0]["Created By"]).WillReturn("User 1");

What happens here it that when the GetAll method from the MyDocuments library iterates through the results collection (e.g. item[0], item[1] etc) TypeMock replaces it with values provided. Using this approach we can add as many items we like to our results and with what ever values we choose.

Using unit tests with development

Unit tests, if used right, can not only help testing but can also help drive development. I’m not going to attempt to describe test driven development (TDD) here because there’s no way I could do it justice. If it’s something you’re interested in then I would definitely recommend looking into it in more detail. What struck me was the advantage of being able to develop code and unit tests alongside each other – allowing both of them to influence the other. Once a piece of development is complete if there’s a series of unit tests already prepared they’re ready to go. I was looking at the code in MyDocuments where it actually creates the individual MyDocument objects, here’s what it does:

   1:  DocumentTitle = item["Title"].ToString(),
   2:  CreatedBy =item["Created By"].ToString(),
   3:  ModifiedBy = item["Modified By"].ToString(),
   4:  Created = Convert.ToDateTime(item["Created"]),
   5:  Modified = Convert.ToDateTime(item["Modified"])

What would happen if a document didn’t have a Modified or Modified By value set? We could try and replicate the data in SharePoint but it would be a lot easier to do with a unit test. I took the test script that was previously written and removed where it sets the Modified and Modified By values (thus making them null). Re-run the test and guess what – it failed! As those fields were null the above code threw an exception. This was a very simple example – it was fairly easy to spot that the code could do with being refactored with some null checks. In real world code these types of potential problems might not be so easy to spot and so having unit tests that can easily be run to flag problems is a great tool.

I’m still learning my way through TypeMock and there may be one or two more posts coming up!

Do you have tips for unit testing in SharePoint? Let us and the SharePoint Baker know.