{"id":1345,"date":"2011-07-11T15:37:27","date_gmt":"2011-07-11T07:37:27","guid":{"rendered":"http:\/\/www.sharepointboost.com\/blog\/?p=1345"},"modified":"2023-07-31T11:40:56","modified_gmt":"2023-07-31T03:40:56","slug":"rough-explanation-of-test-driven-development-tdd-in-sharepoint","status":"publish","type":"post","link":"https:\/\/www.boostsolutions.com\/blog\/rough-explanation-of-test-driven-development-tdd-in-sharepoint\/","title":{"rendered":"Rough Explanation of Test Driven Development (TDD) in SharePoint"},"content":{"rendered":"<p><strong>Background:<\/strong><br \/>\nMany 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.<\/p>\n<p>Write test code before developing feature will bring a stable and high quality architecture.<\/p>\n<p>MSTest is a nice unit test framework, and the Nunit is also a good choice.<br \/>\nThe following is the unit test sketch map:<\/p>\n<p><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2011\/07\/ObjectAndUnitTest.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1346\" src=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2011\/07\/ObjectAndUnitTest.jpg\" alt=\"\" width=\"600\" height=\"409\" srcset=\"https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2011\/07\/ObjectAndUnitTest.jpg 600w, https:\/\/www.boostsolutions.com\/blog\/wp-content\/uploads\/2011\/07\/ObjectAndUnitTest-300x204.jpg 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><a href=\"http:\/\/www.BoostSolutions.com\/blog\/wp-content\/uploads\/2011\/07\/ObjectAndUnitTest.jpg\"><\/a><br \/>\n<strong>Unit testing in normal business object:<\/strong><br \/>\nFor 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.<\/p>\n<p>We will introduce other approach such as IOC, AOP and so on in future to improve the quality and efficiency of SharePoint development.<\/p>\n<p><!--more--><\/p>\n<p>You should use the mock to instead of the object which cost lots of resource.<br \/>\nA mock object is a more evolved and recent version of a fake.<\/p>\n<p>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: <a href=\"http:\/\/sourceforge.net\/projects\/nmock2\">http:\/\/sourceforge.net\/projects\/nmock2<\/a><\/p>\n<p>The code is as following:<\/p>\n<p>\/\/ The BLL interface<br \/>\nnamespace Landpy.SharePointTest.SystemInterface<br \/>\n{<br \/>\npublic interface ILandpySettingReader<br \/>\n{<br \/>\nLandpySetting GetLandpySetting(string url);<br \/>\n}<br \/>\n}<br \/>\n\/\/ The data structure<br \/>\nnamespace Landpy.SharePointTest.SystemInterface<br \/>\n{<br \/>\npublic enum LandpySettingType<br \/>\n{<br \/>\nNormal,<br \/>\nSpecial<br \/>\n}<br \/>\n}<br \/>\nnamespace Landpy.SharePointTest.SystemInterface<br \/>\n{<br \/>\npublic class LandpySetting<br \/>\n{<br \/>\npublic LandpySettingType Type<br \/>\n{<br \/>\nget;<br \/>\nprivate set;<br \/>\n}<\/p>\n<p>public LandpySetting(LandpySettingType type)<br \/>\n{<br \/>\nthis.Type = type;<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n\/\/ The MSTest TestMethod<br \/>\nLandpySetting normalLandpySetting = new LandpySetting(LandpySettingType.Normal);<br \/>\nLandpySetting specialLandpySetting = new LandpySetting(LandpySettingType.Special);<br \/>\nMockery mockery = new Mockery();<br \/>\nILandpySettingReader landpySettingReader = mockery.NewMock&lt;ILandpySettingReader&gt;();<br \/>\nExpect.Once.On(landpySettingReader).Method(&#8220;GetLandpySetting&#8221;).With(&#8220;<a href=\"http:\/\/landpy.cn&quot;).Will(Return.Value(normalLandpySetting\">http:\/\/landpy.cn&#8221;).Will(Return.Value(normalLandpySetting<\/a>));<br \/>\nExpect.Once.On(landpySettingReader).Method(&#8220;GetLandpySetting&#8221;).With(&#8220;<a href=\"http:\/\/landpy.com&quot;).Will(Return.Value(specialLandpySetting\">http:\/\/landpy.com&#8221;).Will(Return.Value(specialLandpySetting<\/a>));<br \/>\nAssert.AreEqual&lt;LandpySettingType&gt;(landpySettingReader.GetLandpySetting(&#8220;<a href=\"http:\/\/landpy.cn&quot;).Type\">http:\/\/landpy.cn&#8221;).Type<\/a>, normalLandpySetting.Type);<br \/>\nAssert.AreEqual&lt;LandpySettingType&gt;(landpySettingReader.GetLandpySetting(&#8220;<a href=\"http:\/\/landpy.com&quot;).Type\">http:\/\/landpy.com&#8221;).Type<\/a>, specialLandpySetting.Type);<\/p>\n<p>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.<\/p>\n<p><strong>Unit testing in SharePoint object:<\/strong><br \/>\nWhat is the difficulty of SharePoint unit test? We need to mock the SharePoint object such as: SPSite, SPList and so on.<\/p>\n<p>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.<\/p>\n<p>You can use SharePoint TypeMock Wrapper to mock the SharePoint object model. Just a pity, the TypeMock is not free.<\/p>\n<p>The codeplex official code is as following:<\/p>\n<p>MockSPSite mockSite = new MockSPSite(&#8220;TestSite&#8221;); MockSPWeb mockWeb = new MockSPWeb(&#8220;TestWeb&#8221;);<br \/>\nMockSPList mockList0 = new MockSPList(&#8220;MyList0&#8221;);<br \/>\nMockSPList mockList1 = new MockSPList(&#8220;MyList1&#8221;);<br \/>\nMockSPList mockList2 = new MockSPList(&#8220;MyList2&#8221;);<br \/>\nmockWeb.Lists = new MockSPListCollection(new[] { mockList0, mockList1, mockList2 });<br \/>\nmockSite.Mock.ExpectGetAlways(&#8220;RootWeb&#8221;, mockWeb.GetInstance());<br \/>\nSPWeb WebMocked = mockWeb.GetInstance();<br \/>\nusing (RecordExpectations recorder = RecorderManager.StartRecording())<br \/>\n{<br \/>\nSPSite SiteMocked = new SPSite(&#8220;&#8221;);<br \/>\nrecorder.ExpectAndReturn(SiteMocked.OpenWeb(), WebMocked);<br \/>\n}<br \/>\n<strong><\/strong><\/p>\n<p><strong>Mock Framework:<\/strong><\/p>\n<ul>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<\/ul>\n<p><strong>References:<\/strong><br \/>\nMicrosoft .NET: Architecting Applications for the Enterprise: Esposito, D. &amp; Saltarello, A.<br \/>\nNMock2: http:\/\/nmock2.sf.net<br \/>\nTypeMock: http:\/\/www.typemock.com\/<br \/>\nSharePoint TypeMock Wrapper: http:\/\/sptypemock.codeplex.com\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[125],"tags":[486,12,140,142,141,139],"_links":{"self":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1345"}],"collection":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1345"}],"version-history":[{"count":26,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1345\/revisions"}],"predecessor-version":[{"id":3889,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1345\/revisions\/3889"}],"wp:attachment":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}