{"id":305,"date":"2011-03-10T21:49:05","date_gmt":"2011-03-10T21:49:05","guid":{"rendered":"http:\/\/www.myfatblog.co.uk\/?p=305"},"modified":"2011-03-11T08:28:36","modified_gmt":"2011-03-11T08:28:36","slug":"part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont","status":"publish","type":"post","link":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/","title":{"rendered":"Part 3&ndash;SharePoint Retreat&ndash;TDD and Unit testing, the methodology used cont.."},"content":{"rendered":"<p>The other parts of this series can be found here\u00e2\u20ac\u00a6<\/p>\n<ol>\n<li><a title=\"SPRetreat TDD \u00e2\u20ac\u201c a retrospective (See what I did there-)\" href=\"http:\/\/www.myfatblog.co.uk\/?p=251\">SPRetreat TDD \u00e2\u20ac\u201c a retrospective (See what I did there? -)<\/a> <\/li>\n<li><a title=\"Part 1 \u00e2\u20ac\u201d SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used\" href=\"http:\/\/www.myfatblog.co.uk\/?p=259\">Part 1 \u00e2\u20ac\u201d SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used<\/a> <\/li>\n<li><a href=\"http:\/\/www.myfatblog.co.uk\/?p=268\">Part 2 &#8212; SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used cont..<\/a> <\/li>\n<li>Part 3 -\u00e2\u20ac\u201c SharePoint Retreat-TDD and Unit Testing, the methodology used cont..&#160; (This post) <\/li>\n<\/ol>\n<h2>Moving into the realms of SharePoint<\/h2>\n<p>In the previous episode, we created our Magic8Ball object using TDD techniques to drive the creation of the Objects, Properties, Fields and methods. We now need to take that completed object and look at how we bring it into the SharePoint environment, utilising TDD whre possible. (<em>Note: I\u00e2\u20ac\u2122m focussing on TDD here, not SharePoint, therefore this is NOT production standard code.<\/em>)<\/p>\n<p>We didn\u00e2\u20ac\u2122t get quite as far as getting our Magic8Balls fully into SharePoint on the retreat, other than to use TypeMock to isolate us from calls to the object model. Therefore this part will hopefully generate a bit of retrospective discussion as I\u00e2\u20ac\u2122m going to try and adapt what I learnt about TDD on the Retreat and apply it here, without the benefit of the retrospectives we held.<\/p>\n<p>If I get this wrong, it will in any case reinforce the need to hold Agile retrospectives when possible! <img decoding=\"async\" style=\"border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none\" class=\"wlEmoticon wlEmoticon-winkingsmile\" alt=\"Winking smile\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png\" \/><\/p>\n<p>The important thing to remember is that we don\u00e2\u20ac\u2122t need to use TDD on everything in this project. We\u00e2\u20ac\u2122re going to use the supplied distribution framework from MS for instance, and one hopes that they\u00e2\u20ac\u2122ve tested this thoroughly, therefore we won\u00e2\u20ac\u2122t.. Crossing the Seams\u00e2\u20ac\u00a6 remember?<a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" align=\"right\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb.png\" width=\"185\" height=\"294\" \/><\/a><\/p>\n<p>In order to help this process, I\u00e2\u20ac\u2122m going to create a new SharePoint aware class that inherits from Magic8Ball. I can then use this class simply in a Sharepoint web part project, keeping the logic and display separation as much as possible. We\u00e2\u20ac\u2122ll add the corresponding test class to the test project at the same time.<\/p>\n<p>Remember to decorate your test class with the TestFixture attribute so that NUnit knows it has tests in it. Let Resharper handle the Using statements for you, it\u00e2\u20ac\u2122s quicker!<\/p>\n<p><a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_3.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb_3.png\" width=\"306\" height=\"163\" \/><\/a><\/p>\n<p>At this point, I forgot to make the SharePoint8Ball class public, but Resharper spotted that and offers to fix it..<\/p>\n<p><a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb_4.png\" width=\"280\" height=\"107\" \/><\/a><\/p>\n<p>We\u00e2\u20ac\u2122ve allowed Resharper to do this and we can now reference the class in our test code.<\/p>\n<pre class=\"csharpcode\">            SharePoint8Ball spball = <span class=\"kwrd\">new<\/span> SharePoint8Ball();\r\n            spball.Answers = <span class=\"kwrd\">new<\/span> List&lt;<span class=\"kwrd\">string<\/span>&gt;() {<span class=\"str\">&quot;a&quot;<\/span>, <span class=\"str\">&quot;b&quot;<\/span>, <span class=\"str\">&quot;c&quot;<\/span>};<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>At the moment though, we have a compile failure as the spball object doesn\u00e2\u20ac\u2122t have an Answers property. In this case though, Resharper can\u00e2\u20ac\u2122t help us at it\u00e2\u20ac\u2122s only suggesting that we create a new property or field. What we actually want to do is have the SharePoint8Ball class inherit from the Magic8Ball class. As this is a compile failure, it\u00e2\u20ac\u2122s perfectly valid in the TDD mantra to switch to the production code and correct this.<\/p>\n<pre class=\"csharpcode\">    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> SharePoint8Ball : Ball\r\n    {\r\n    }<\/pre>\n<p>This fixes our compile failure as the spball now has an Answers property. We\u00e2\u20ac\u2122ll finish our test code by repeating an earlier test, but this time with the new object.<\/p>\n<pre class=\"csharpcode\">        [Test]\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> Create_NewSP8ball_ReturnsValidBall()\r\n        {\r\n            <span class=\"rem\">\/\/Arrange<\/span>\r\n            SharePoint8Ball spball = <span class=\"kwrd\">new<\/span> SharePoint8Ball();\r\n            spball.Answers = <span class=\"kwrd\">new<\/span> List&lt;<span class=\"kwrd\">string<\/span>&gt;() {<span class=\"str\">&quot;a&quot;<\/span>, <span class=\"str\">&quot;b&quot;<\/span>, <span class=\"str\">&quot;c&quot;<\/span>};\r\n\r\n            <span class=\"rem\">\/\/Act<\/span>\r\n            <span class=\"kwrd\">string<\/span> answers = spball.AskQuestion(<span class=\"str\">&quot;q&quot;<\/span>);\r\n\r\n            <span class=\"rem\">\/\/Assert<\/span>\r\n            Assert.Contains(answers,spball.Answers);\r\n\r\n        }<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>Run all the tests and you should get all passed.<\/p>\n<pre class=\"csharpcode\">------ Test started: Assembly: Magic8Ball.Test.dll ------\r\n\r\n11 passed, 0 failed, 0 skipped, took 12.46 seconds (NUnit 2.5.5).<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>Our next step is to think about the new SharePoint8Ball and how it differs from it\u00e2\u20ac\u2122s Parent the Magic8Ball. We still have exactly the same demands of the object, We want to supply a list of Answers, We want to ask a&#160; question and we want a single life changing answer (well an answer at least..) We don\u00e2\u20ac\u2122t need to retest everything that we tested previously as we know that we\u00e2\u20ac\u2122re talking to the parent object happily.&#160; So what\u00e2\u20ac\u2122s different?<\/p>\n<ul>\n<li>We want to talk to a SharePoint Web. <\/li>\n<li>We want to get the answers from a List. <\/li>\n<li>The administrator will provide the List name in the web part properties. <\/li>\n<\/ul>\n<p>So, at a bare minimum we need to handle an SPWeb object and a Listname. We\u00e2\u20ac\u2122ll start with the SPWeb. As we\u00e2\u20ac\u2122re planning to use this object in a Web Part, we can use the SPContext object to get access to the current Web object.<\/p>\n<blockquote>\n<p>Just as an aside, I found myself starting to cut and paste the Setup code this time round for the SharePoint8Ball, so it\u00e2\u20ac\u2122s time to refactor, a step I missed after the first test! So we\u00e2\u20ac\u2122ll quickly make use of the NUnit [Setup] tag. this denotes a block of code to be run before every test in that class.<\/p>\n<p>At the top of our SharePoint8BallTest class we\u00e2\u20ac\u2122ll add a private field for SharePoint8Ball spball, We\u00e2\u20ac\u2122ll add a new Method called SetUp which will be decorated with the [SetUp] attribute and we\u00e2\u20ac\u2122ll refactor out the \/\/Arrange frm our previous test, giving us the code below.<\/p>\n<pre class=\"csharpcode\">        <span class=\"kwrd\">private<\/span> SharePoint8Ball spball;\r\n        \r\n        [SetUp]\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> SetUp()\r\n        {\r\n            <span class=\"rem\">\/\/Arrange<\/span>\r\n            spball = <span class=\"kwrd\">new<\/span> SharePoint8Ball();\r\n            spball.Answers = <span class=\"kwrd\">new<\/span> List&lt;<span class=\"kwrd\">string<\/span>&gt;() {<span class=\"str\">&quot;a&quot;<\/span>, <span class=\"str\">&quot;b&quot;<\/span>, <span class=\"str\">&quot;c&quot;<\/span>};\r\n        }\r\n\r\n        [Test]\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> Create_NewSP8ball_ReturnsValidBall()\r\n        {\r\n            <span class=\"rem\">\/\/Act<\/span>\r\n            <span class=\"kwrd\">string<\/span> answers = spball.AskQuestion(<span class=\"str\">&quot;q&quot;<\/span>);\r\n\r\n            <span class=\"rem\">\/\/Assert<\/span>\r\n            Assert.Contains(answers,spball.Answers);\r\n\r\n        }<\/pre>\n<p>Running our test as before passes.<\/p>\n<\/blockquote>\n<p>Where were we? Ah yes we want to get an SPWeb object into a SharePoint8Ball property. Time for another test.<\/p>\n<p>As we already have an \/\/Arrange section in the set-up and don\u00e2\u20ac\u2122t need to add anything this is a very short test as the \/\/Act is also in the assert.<\/p>\n<pre class=\"csharpcode\">        [Test]\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> Create_NewSP8Ball_ReturnsValidSPWeb()\r\n        {\r\n            <span class=\"rem\">\/\/Arrange<\/span>\r\n            <span class=\"rem\">\/\/Act<\/span>\r\n            <span class=\"rem\">\/\/Assert<\/span>\r\n            Assert.IsNotNull(spball.Web);\r\n        }<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>Straight away we have a compile failure on the spball.Web object as it doesn\u00e2\u20ac\u2122t exist. I\u00e2\u20ac\u2122m going to fix this by allowing Resharper to create a Read-only property as I want to create the SPWeb in the constructor for the SharePoint8Ball class.<\/p>\n<p><a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_5.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb_5.png\" width=\"402\" height=\"165\" \/><\/a><\/p>\n<p>Resharper creates our Public read-only property and then I\u00e2\u20ac\u2122ve added a private field to sit behind. but, we\u00e2\u20ac\u2122ve hit a compile error as the Class isn\u00e2\u20ac\u2122t currently aware of the SharePoint namespace yet.<\/p>\n<p><a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb_6.png\" width=\"412\" height=\"157\" \/><\/a><\/p>\n<p>Unfortunately Resharper can\u00e2\u20ac\u2122t fix this just yet as we don\u00e2\u20ac\u2122t have a reference to Microsoft.SharePoint.dll in our class. Once we add the reference, we can let\u00e2\u20ac\u2122s Resharper add the using statements.<\/p>\n<p><a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb_7.png\" width=\"319\" height=\"79\" \/><\/a><\/p>\n<p>Running the test returns a fail as we hit an exception. Fix that by changing the Property to return the internal web field. Run the test again and this time it fails with a null reference exception. To fix this one, we need to get busy on the SharePoint8Ball constructor itself. As we\u00e2\u20ac\u2122re planning to use the SPContext object we can add a simple constructor that takes a reference to the SPContext.Current.Web object and places this in the internal .Web field.<\/p>\n<pre class=\"csharpcode\">        <span class=\"kwrd\">public<\/span> SharePoint8Ball()\r\n        {\r\n            web = SPContext.Current.Web;\r\n        }<\/pre>\n<p>At this point, running the test again still results in a Null reference exception, this time however we know that it\u00e2\u20ac\u2122s because we\u00e2\u20ac\u2122re running outside of the Web environment, therefore SPContext doesn\u00e2\u20ac\u2122t exist. To test this we\u00e2\u20ac\u2122ll have to use TypeMock to fake the objects.<\/p>\n<p>In our failing test, we need to add some extra code to the \/\/Arrange portion of our failing test. Start by typing SPWeb and let Resharper add both the reference and the using statements this time.<\/p>\n<p><a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb_8.png\" width=\"358\" height=\"85\" \/><\/a><\/p>\n<p>Add an object name fakeWeb = Isolate. and then let Resharper do some work again, this time adding the Using statement for TypeMock.<\/p>\n<p><a href=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/image_thumb_9.png\" width=\"370\" height=\"70\" \/><\/a><\/p>\n<p>We&#8217;re all set up now for the faking of SharePoint objects so the code block below can be added.<\/p>\n<pre class=\"csharpcode\">        [Test]\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> Create_NewSP8Ball_ReturnsValidSPWeb()\r\n        {\r\n            <span class=\"rem\">\/\/Arrange            <\/span>\r\n            SPWeb fakeWeb = Isolate.Fake.Instance&lt;SPWeb&gt;();\r\n            Isolate.WhenCalled(() =&gt; SPContext.Current.Web).WillReturn(fakeWeb);\r\n\r\n            <span class=\"rem\">\/\/Act<\/span>\r\n            <span class=\"rem\">\/\/Assert<\/span>\r\n            Assert.IsNotNull(spball.Web);\r\n        }<\/pre>\n<p>With this code in and running the test, I hit a problem. I got a Null Reference exception that I wasn\u00e2\u20ac\u2122t expecting. This should have worked fine. After a quick debug, I realised that my SetUp code was of course calling the new constructor before the typeMock and as a result was failing because it couldn\u00e2\u20ac\u2122t see the faked SPContext object this early on. This mean\u00e2\u20ac\u2122t both tests were failing.<\/p>\n<p>Therefore as I\u00e2\u20ac\u2122d also need the faked SPContext object in the first test now, I refactored the return of fakeWeb into my setup method and moved the creation of the Answers string list into Test one as it needs to overide anything we set-up in the constructor.<\/p>\n<pre class=\"csharpcode\">        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> SetUp()\r\n        {\r\n            <span class=\"rem\">\/\/Arrange<\/span>\r\n            SPWeb fakeWeb = Isolate.Fake.Instance&lt;SPWeb&gt;();\r\n            Isolate.WhenCalled(() =&gt; SPContext.Current.Web).WillReturn(fakeWeb);\r\n\r\n            spball = <span class=\"kwrd\">new<\/span> SharePoint8Ball();\r\n        }<\/pre>\n<p>Note the order in this code, the Faked objects are constructed BEFORE the SharePoint8Ball. Run the tests again and both pass. <\/p>\n<blockquote>\n<p>By hitting this problem, I realised that I\u00e2\u20ac\u2122m going to have to do ALL of my TypeMock setting up in the setup method now, as I\u00e2\u20ac\u2122ll be making changes to the constructor as I go which will affect both tests. the alternative would be to mark the first test as ignore, remove the Setup and place it all back in the tests again.<\/p>\n<p>I decided that as I want to perhaps vary some of the later tests, I\u00e2\u20ac\u2122d ensure all the mocks are configured in the Setup.<\/p>\n<p><em>At this point, I\u00e2\u20ac\u2122d played with the test code quite a bit whilst I worked out how to do this. What was quite reassuring was how easy it was to just run all the tests on the project once I got back to the code state I thought I wanted to be in. Seeing all the unit tests pass was a great confidence booster.<\/em><\/p>\n<\/blockquote>\n<p>So for the next test, I want to retrieve answers from a SharePoint list and place these in the spball.Answers object. To do this, we\u00e2\u20ac\u2122re going to need to fake a couple of new items. We need a fakeList and a fakeItem.<\/p>\n<p>We\u00e2\u20ac\u2122ll add the following code to our SetUp between the creation of the fakeWeb and the initialisation of our spball object.<\/p>\n<pre class=\"csharpcode\">            SPList fakeList = Isolate.Fake.Instance&lt;SPList&gt;();\r\n            Isolate.WhenCalled(() =&gt; fakeWeb.Lists[<span class=\"str\">&quot;Listname&quot;<\/span>]).WillReturn(fakeList);\r\n\r\n            SPListItem fakeItem = Isolate.Fake.Instance&lt;SPListItem&gt;();\r\n            Isolate.WhenCalled(() =&gt; fakeItem.Title).WillReturn(<span class=\"str\">&quot;z&quot;<\/span>);\r\n            Isolate.WhenCalled(() =&gt; fakeList.Items).WillReturnCollectionValuesOf(<span class=\"kwrd\">new<\/span> List&lt;SPListItem&gt;\r\n            {\r\n                fakeItem\r\n            });<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>We then create our test against the Answers object from SharePoint.<\/p>\n<pre class=\"csharpcode\">        [Test]\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">void<\/span> Create_NewSP8Ball_RetrieveAnswersFromSPList()\r\n        {\r\n            <span class=\"rem\">\/\/Arrange<\/span>\r\n            <span class=\"rem\">\/\/Act<\/span>\r\n            <span class=\"rem\">\/\/Assert<\/span>\r\n            Assert.IsNotNull(spball.Answers);\r\n        }<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>Run the test and as expected we get a failure as Answers is still null. So we\u00e2\u20ac\u2122ll fix that by changing the constructor logic in the SharePoint8Ball class. As we add the code below, we know we\u00e2\u20ac\u2122re going to need to pass in a parameter for listname frm the webpart, so we\u00e2\u20ac\u2122ll include that too.<\/p>\n<pre class=\"csharpcode\">        <span class=\"kwrd\">public<\/span> SharePoint8Ball(<span class=\"kwrd\">string<\/span> listname)\r\n        {\r\n            web = SPContext.Current.Web;\r\n            SPList sourceList = web.Lists[listname];\r\n\r\n            SPListItemCollection itemCol = sourceList.Items;\r\n\r\n            List&lt;<span class=\"kwrd\">string<\/span>&gt; answers = <span class=\"kwrd\">new<\/span> List&lt;<span class=\"kwrd\">string<\/span>&gt;();\r\n\r\n            <span class=\"kwrd\">foreach<\/span> (SPListItem item <span class=\"kwrd\">in<\/span> itemCol)\r\n            {\r\n                answers.Add(item.Title);\r\n            }\r\n\r\n            <span class=\"kwrd\">base<\/span>.Answers = answers;\r\n\r\n        }<\/pre>\n<p>Running the test we hit a compile error as the constructor called in our setup method is now expecting a parameter for the listname.<\/p>\n<pre class=\"csharpcode\">            spball = <span class=\"kwrd\">new<\/span> SharePoint8Ball(<span class=\"str\">&quot;Listname&quot;<\/span>);<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>Running the tests now all pass.<\/p>\n<p>At this point, I\u00e2\u20ac\u2122m happy that the SharePoint version of our Magic8Ball is now ready to receive items from SharePoint in the next part, I\u00e2\u20ac\u2122ll drop it into a Web Part and run it in a SharePoint web page. <\/p>\n<p>Paul<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other parts of this series can be found here\u00e2\u20ac\u00a6 SPRetreat TDD \u00e2\u20ac\u201c a retrospective (See what I did there? -) Part 1 \u00e2\u20ac\u201d SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used Part 2 &#8212; SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used cont.. Part 3 -\u00e2\u20ac\u201c SharePoint Retreat-TDD and Unit Testing, the methodology used &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[43,42,13,31,35],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Part 3&ndash;SharePoint Retreat&ndash;TDD and Unit testing, the methodology used cont.. - Blog of an overweight SharePoint addict<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Part 3&ndash;SharePoint Retreat&ndash;TDD and Unit testing, the methodology used cont.. - Blog of an overweight SharePoint addict\" \/>\n<meta property=\"og:description\" content=\"The other parts of this series can be found here\u00e2\u20ac\u00a6 SPRetreat TDD \u00e2\u20ac\u201c a retrospective (See what I did there? -) Part 1 \u00e2\u20ac\u201d SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used Part 2 &#8212; SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used cont.. Part 3 -\u00e2\u20ac\u201c SharePoint Retreat-TDD and Unit Testing, the methodology used &hellip; Continue reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog of an overweight SharePoint addict\" \/>\n<meta property=\"article:published_time\" content=\"2011-03-10T21:49:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2011-03-11T08:28:36+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png\" \/>\n<meta name=\"author\" content=\"Cimares\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cimares\" \/>\n<meta name=\"twitter:site\" content=\"@cimares\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Cimares\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/\",\"url\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/\",\"name\":\"Part 3&ndash;SharePoint Retreat&ndash;TDD and Unit testing, the methodology used cont.. - Blog of an overweight SharePoint addict\",\"isPartOf\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png\",\"datePublished\":\"2011-03-10T21:49:05+00:00\",\"dateModified\":\"2011-03-11T08:28:36+00:00\",\"author\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/#primaryimage\",\"url\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png\",\"contentUrl\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png\"},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#website\",\"url\":\"http:\/\/www.myfatblog.co.uk\/\",\"name\":\"Blog of an overweight SharePoint addict\",\"description\":\"The rantings of a (not so) food obsessed IT consultant!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.myfatblog.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6\",\"name\":\"Cimares\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg\",\"contentUrl\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg\",\"caption\":\"Cimares\"},\"sameAs\":[\"http:\/\/www.myfatblog.co.uk\"],\"url\":\"http:\/\/www.myfatblog.co.uk\/index.php\/author\/reginald\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Part 3&ndash;SharePoint Retreat&ndash;TDD and Unit testing, the methodology used cont.. - Blog of an overweight SharePoint addict","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/","og_locale":"en_US","og_type":"article","og_title":"Part 3&ndash;SharePoint Retreat&ndash;TDD and Unit testing, the methodology used cont.. - Blog of an overweight SharePoint addict","og_description":"The other parts of this series can be found here\u00e2\u20ac\u00a6 SPRetreat TDD \u00e2\u20ac\u201c a retrospective (See what I did there? -) Part 1 \u00e2\u20ac\u201d SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used Part 2 &#8212; SharePoint Retreat\u00e2\u20ac\u201cTDD and Unit Testing, the methodology used cont.. Part 3 -\u00e2\u20ac\u201c SharePoint Retreat-TDD and Unit Testing, the methodology used &hellip; Continue reading","og_url":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/","og_site_name":"Blog of an overweight SharePoint addict","article_published_time":"2011-03-10T21:49:05+00:00","article_modified_time":"2011-03-11T08:28:36+00:00","og_image":[{"url":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png"}],"author":"Cimares","twitter_card":"summary_large_image","twitter_creator":"@cimares","twitter_site":"@cimares","twitter_misc":{"Written by":"Cimares","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/","url":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/","name":"Part 3&ndash;SharePoint Retreat&ndash;TDD and Unit testing, the methodology used cont.. - Blog of an overweight SharePoint addict","isPartOf":{"@id":"http:\/\/www.myfatblog.co.uk\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/#primaryimage"},"image":{"@id":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/#primaryimage"},"thumbnailUrl":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png","datePublished":"2011-03-10T21:49:05+00:00","dateModified":"2011-03-11T08:28:36+00:00","author":{"@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.myfatblog.co.uk\/index.php\/2011\/03\/part-3sharepoint-retreattdd-and-unit-testing-the-methodology-used-cont\/#primaryimage","url":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png","contentUrl":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/868d0729f955_656F\/wlEmoticon-winkingsmile.png"},{"@type":"WebSite","@id":"http:\/\/www.myfatblog.co.uk\/#website","url":"http:\/\/www.myfatblog.co.uk\/","name":"Blog of an overweight SharePoint addict","description":"The rantings of a (not so) food obsessed IT consultant!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.myfatblog.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6","name":"Cimares","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/image\/","url":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg","contentUrl":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg","caption":"Cimares"},"sameAs":["http:\/\/www.myfatblog.co.uk"],"url":"http:\/\/www.myfatblog.co.uk\/index.php\/author\/reginald\/"}]}},"_links":{"self":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/305"}],"collection":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":17,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":322,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions\/322"}],"wp:attachment":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}