views:

177

answers:

4

I have a web part that uses PortalSiteMapProvider to query the Sharepoint navigation hierarchy, but unit tests written for that code fail, because the code is being run outside the Sharepoint context and thus no site map providers are available.

I've identified two alternate ways of solving this dilemma:

  • Because Sharepoint is basically an elaborate ASP.Net application, it should be possible to run tests inside the Sharepoint context with the HostType and UrlToTest test attributes
  • Use a mock instead of PortalSiteMapProvider

Are either of these viable or is there a better third option?

A: 

Second option is more appropriate. Abstract away PSMP and hide it behind IPortalSiteMapProvider and then mock it in your unit test. In order to bridge the interface and concrete implementation you can either write a thin delegating adapter or use duck typing.

Anton Gogolev
Urgh, Sharepoint and unit testing is always horrible. Because there are no interfaces for any sharepoint classes you'll have to abstract the PSMP through another custom class just to unit test.
Ray Booysen
+2  A: 

The Microsoft Patterns and Practices dudes recommend TypeMock to help unit test Sharepoint

http://msdn.microsoft.com/en-us/library/dd203468.aspx

http://www.typemock.com/sharepointpage.php

Not a free solution unfortunately.

Aidan
A: 

You will not be able to mock the SPRequest class, which is an internal class. I am facing the same issues. One approach is to try to isolate your code from the SharePoint API, and this is not so nice.

Sergiu
A: 

BTW Typemock have a reduced price product especially for SharePoint.