Background:
Many teams use XP Agile Development. Test driven development (TDD) is a software development process that relies on the repetition of a very short development cycle.
Write test code before developing feature will bring a stable and high quality architecture.
MSTest is a nice unit test framework, and the Nunit is also a good choice.
The following is the unit test sketch map:
Unit testing in normal business object:
For OOA, OOD and OOP, programming to an interface is a good approach. Every object is isolated and the interface make a role as relationship between the objects. We can use Dependency Injection with constructor to build relationships between the objects.
We will introduce other approach such as IOC, AOP and so on in future to improve the quality and efficiency of SharePoint development.
You should use the mock to instead of the object which cost lots of resource.
A mock object is a more evolved and recent version of a fake.
The NMock2 framework is an open-source library providing a dynamic framework for .NET interfaces. The mock object uses strings to get input and reflection to set expectations. You can reference the link: http://sourceforge.net/projects/nmock2
The code is as following:
// The BLL interface
namespace Landpy.SharePointTest.SystemInterface
{
public interface ILandpySettingReader
{
LandpySetting GetLandpySetting(string url);
}
}
// The data structure
namespace Landpy.SharePointTest.SystemInterface
{
public enum LandpySettingType
{
Normal,
Special
}
}
namespace Landpy.SharePointTest.SystemInterface
{
public class LandpySetting
{
public LandpySettingType Type
{
get;
private set;
}
public LandpySetting(LandpySettingType type)
{
this.Type = type;
}
}
}
// The MSTest TestMethod
LandpySetting normalLandpySetting = new LandpySetting(LandpySettingType.Normal);
LandpySetting specialLandpySetting = new LandpySetting(LandpySettingType.Special);
Mockery mockery = new Mockery();
ILandpySettingReader landpySettingReader = mockery.NewMock<ILandpySettingReader>();
Expect.Once.On(landpySettingReader).Method(“GetLandpySetting”).With(“http://landpy.cn”).Will(Return.Value(normalLandpySetting));
Expect.Once.On(landpySettingReader).Method(“GetLandpySetting”).With(“http://landpy.com”).Will(Return.Value(specialLandpySetting));
Assert.AreEqual<LandpySettingType>(landpySettingReader.GetLandpySetting(“http://landpy.cn”).Type, normalLandpySetting.Type);
Assert.AreEqual<LandpySettingType>(landpySettingReader.GetLandpySetting(“http://landpy.com”).Type, specialLandpySetting.Type);
You can see that there is no implement about the interface, the mock object is visual object and it can instead of the object which cost lots of resource.
Unit testing in SharePoint object:
What is the difficulty of SharePoint unit test? We need to mock the SharePoint object such as: SPSite, SPList and so on.
Program to an interface in the business logic layer(BLL), we use mock technique to invoke the fake object to instead of the SharePoint object model.
You can use SharePoint TypeMock Wrapper to mock the SharePoint object model. Just a pity, the TypeMock is not free.
The codeplex official code is as following:
MockSPSite mockSite = new MockSPSite(“TestSite”); MockSPWeb mockWeb = new MockSPWeb(“TestWeb”);
MockSPList mockList0 = new MockSPList(“MyList0”);
MockSPList mockList1 = new MockSPList(“MyList1”);
MockSPList mockList2 = new MockSPList(“MyList2”);
mockWeb.Lists = new MockSPListCollection(new[] { mockList0, mockList1, mockList2 });
mockSite.Mock.ExpectGetAlways(“RootWeb”, mockWeb.GetInstance());
SPWeb WebMocked = mockWeb.GetInstance();
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
SPSite SiteMocked = new SPSite(“”);
recorder.ExpectAndReturn(SiteMocked.OpenWeb(), WebMocked);
}
Mock Framework:
- NMock2 is an open-source library providing a dynamic mocking framework for .NET interfaces. The mock object uses strings to get input and reflection to set expectations.
- TypeMock is a commercial product with capabilities that basically don not require you to design your code for testability. TypeMock enables testing code that was previously considered untestable, such as static methods, non virtual methods and sealed classes.
- Rhino Mocks is an open-source product. Through a wizard, it generates a static mock class for type-safe testing. You set mock expectations by accessing directly the mocked object, rather than going through one more level of indirection.
References:
Microsoft .NET: Architecting Applications for the Enterprise: Esposito, D. & Saltarello, A.
NMock2: http://nmock2.sf.net
TypeMock: http://www.typemock.com/
SharePoint TypeMock Wrapper: http://sptypemock.codeplex.com/