<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>C h a k s' C o r n e r &#187; TDD</title>
	<atom:link href="http://chakkaradeep.wordpress.com/category/tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://chakkaradeep.wordpress.com</link>
	<description>Enjoy Computing!</description>
	<lastBuildDate>Tue, 07 Apr 2009 01:26:00 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='chakkaradeep.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/dec6c8242164cf113e53d8d9af323b7e?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>C h a k s' C o r n e r &#187; TDD</title>
		<link>http://chakkaradeep.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://chakkaradeep.wordpress.com/osd.xml" title="C h a k s&#8217; C o r n e r" />
		<item>
		<title>Team System Unit Test and Output Directories</title>
		<link>http://chakkaradeep.wordpress.com/2008/07/14/team-system-unit-test-and-output-directories/</link>
		<comments>http://chakkaradeep.wordpress.com/2008/07/14/team-system-unit-test-and-output-directories/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 05:49:19 +0000</pubDate>
		<dc:creator>Chaks</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://chakkaradeep.wordpress.com/?p=626</guid>
		<description><![CDATA[I always use NUnit to write my unit tests but now at Intergen I also use Microsoft Visual Studio Team System Unit Tests. Today I was stuck by a very basic problem and here it is.
I have a XML file in my project which is added to the output directory when compiled.

When I was running [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=626&subd=chakkaradeep&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I always use <a href="http://nunit.org/index.php" target="_blank">NUnit</a> to write my unit tests but now at <a href="http://intergen.co.nz" target="_blank">Intergen</a> I also use Microsoft <a href="http://msdn.microsoft.com/en-us/vsts2008/aa718823.aspx" target="_blank">Visual Studio Team System Unit Tests</a>. Today I was stuck by a very basic problem and here it is.</p>
<p>I have a XML file in my project which is added to the <em>output directory</em> when compiled.</p>
<p><a href="http://chakkaradeep.files.wordpress.com/2008/07/birthxml-properties.png"><img class="alignnone size-full wp-image-627" src="http://chakkaradeep.files.wordpress.com/2008/07/birthxml-properties.png?w=286&#038;h=283" alt="" width="286" height="283" /></a></p>
<p>When I was running my unit tests against this, Visual Studio threw me this error saying that it couldn&#8217;t find the file!</p>
<p><a href="http://chakkaradeep.files.wordpress.com/2008/07/test-result-fails.png"><img class="alignnone size-medium wp-image-628" src="http://chakkaradeep.files.wordpress.com/2008/07/test-result-fails.png?w=300&#038;h=68" alt="" width="300" height="68" /></a></p>
<p>(click to enlarge)</p>
<p>I was really surprised as my XML file is said to copy to output directory and the test fails saying that there is no such file. Checked my Test Solution&#8217;s output directory and found that there is indeed the <em>Templates</em> folder and the XML file.</p>
<p><a href="http://chakkaradeep.files.wordpress.com/2008/07/birth-tests-output-dir.png"><img class="alignnone size-medium wp-image-629" src="http://chakkaradeep.files.wordpress.com/2008/07/birth-tests-output-dir.png?w=300&#038;h=147" alt="" width="300" height="147" /></a></p>
<p>So, whats the problem? Why is my test failing?</p>
<p>Well, the VSTE has a separate directory where it stores all its test results. The directory can be found in your root directory of your solution under the name <em>TestResults</em>. The <em>TestResults </em>also have various folders for each test run you do. If you refer back to the error image that is attached above, you could get the exact directory name. So, the VSTE did not copy the <em>Templates </em>directory to this &#8216;dynamic&#8217; (for each test run there will be a directory created : <em>correct me if am wrong here</em>) directory. How do we do this then?</p>
<p>The answer is with the <em>Local Test Run Config&#8217;s Deployment settings</em>. You can find the local test run config in your solution browser.</p>
<p><a href="http://chakkaradeep.files.wordpress.com/2008/07/config-file.png"><img class="alignnone size-full wp-image-631" src="http://chakkaradeep.files.wordpress.com/2008/07/config-file.png?w=203&#038;h=74" alt="" width="203" height="74" /></a></p>
<p>Double click the file to open the properties dialog and go to the Deployment section.</p>
<p><a href="http://chakkaradeep.files.wordpress.com/2008/07/deployment.png"><img class="alignnone size-full wp-image-630" src="http://chakkaradeep.files.wordpress.com/2008/07/deployment.png?w=510&#038;h=362" alt="" width="510" height="362" /></a></p>
<p>Specifying a file or directory here would be included along with the test results <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>My test doesnt fail now <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> </p>
<p>Another way to do this (if its a single file : <em>if you know how to add for a directory, please do leave your comment</em> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) is to specify along with your test method,</p>
<p><strong><span style="color:#ff0000;">[TestMethod, DeploymentItem(@"Templates\BirthInputTemplate.xml")]</span></strong></p>
<p><strong><span style="color:#ff0000;">public void Test_BuildInputXML()<br />
{<br />
}</span><br />
</strong></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/chakkaradeep.wordpress.com/626/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/chakkaradeep.wordpress.com/626/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chakkaradeep.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chakkaradeep.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chakkaradeep.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chakkaradeep.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chakkaradeep.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chakkaradeep.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chakkaradeep.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chakkaradeep.wordpress.com/626/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chakkaradeep.wordpress.com/626/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chakkaradeep.wordpress.com/626/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=626&subd=chakkaradeep&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chakkaradeep.wordpress.com/2008/07/14/team-system-unit-test-and-output-directories/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/94894baff370de381988daaa0011ee42?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">Chaks</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/07/birthxml-properties.png" medium="image" />

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/07/test-result-fails.png?w=300" medium="image" />

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/07/birth-tests-output-dir.png?w=300" medium="image" />

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/07/config-file.png" medium="image" />

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/07/deployment.png" medium="image" />
	</item>
		<item>
		<title>Mocking Events [With RhinoMocks]</title>
		<link>http://chakkaradeep.wordpress.com/2008/04/03/mocking-events-with-rhinomocks/</link>
		<comments>http://chakkaradeep.wordpress.com/2008/04/03/mocking-events-with-rhinomocks/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 10:37:26 +0000</pubDate>
		<dc:creator>Chaks</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://chakkaradeep.wordpress.com/2008/04/03/mocking-events-with-rhinomocks/</guid>
		<description><![CDATA[Suppose we have an Inerface called IFooDatabase . Think this Interface as our gateway which will provide us with basic database operations.
Now, how does Mocking come here?  &#8211; My friend is still working on this database layer but he has given me already the Interface with which he is implementing the Database Class, so [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=552&subd=chakkaradeep&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Suppose we have an Inerface called <i>IFooDatabase</i> . Think this Interface as our gateway which will provide us with basic database operations.</p>
<p>Now, how does Mocking come here?  &#8211; My friend is still working on this database layer but he has given me already the Interface with which he is implementing the Database Class, so that I can make use of that and <i>mock,</i> as if that database layer exists and test my program against it.</p>
<p>What mocking framework I am using? &#8211; <a href="http://www.ayende.com/projects/rhino-mocks.aspx">RhinoMocks</a></p>
<p>My friend told me that he is going to add events for each database operation, like <i>InsertEvent</i>, <i>UpdateEvent</i>, <i>DeleteEvent</i> so that they may be raised for each <i>insertion</i>, <i>updation</i> and <i>deletion</i> operations respectively.</p>
<p>For this post, to make it simple, let me take just the <i>InsertEvent</i>. Here is the class diagram,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/04/040208-2137-mockingeven1.png" /></p>
<p>And here is my Class which is going to make use of this Interface</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/04/040208-2137-mockingeven2.png" /></p>
<p>I was pretty happy that my friend provided me an Interface to mock all the database activities, but I was really stuck on how do I test all those Events are raised!</p>
<p>Well, been using one of the best Mocking Frameworks around, RhinoMocks did provided a way to mock Events <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So, here is my test case,</p>
<p><span style="font-family:Courier New;font-size:10pt;">[<span style="color:#2b91af;">Test</span>]<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">public </span><span style="color:blue;">void</span> TestInsertEventRaised()</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">{<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">MockRepository</span> fooDatabaseMock = <span style="color:blue;">new</span><br />
<span style="color:#2b91af;">MockRepository</span>();<br />
<span style="color:#2b91af;"></span></span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">IFooDatabase</span> </span></p>
<p><span style="font-family:Courier New;font-size:10pt;">   fooDatabase = fooDatabaseMock.DynamicMock&lt;<span style="color:#2b91af;">IFooDatabase</span>&gt;();</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">fooDatabase.FooDatabaseInsertEvent += <span style="color:blue;">null</span>;<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">LastCall</span>.IgnoreArguments();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">IEventRaiser</span><br />
fooEventRaiser = <span style="color:#2b91af;">LastCall</span>.GetEventRaiser();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    fooDatabaseMock.ReplayAll();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">Foo</span> myFoo = <span style="color:blue;">new</span><br />
<span style="color:#2b91af;">Foo</span>(fooDatabase);<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    fooEventRaiser.Raise(<span style="color:blue;">this</span>, <span style="color:#2b91af;">EventArgs</span>.Empty);<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">Assert</span>.IsTrue(myFoo.InsertEventRaised);<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">}</span></p>
<p>RhinoMocks provides an interface called IEventRaiser which you can use to raise events!</p>
<p>Here is what we are doing,</p>
<ul>
<li>I am assigning <i>null </i>to the EventHandler and telling RhinoMocks that please do ignore the arguments in my last call. This helps us because we may associate our event to any Event Handler with different event arguments</li>
<li>We initialize IEventRaiser by getting the last event, which in our case is <i>FooDatabaseInsertEvent</i></li>
<li>We pass on the Database object which is mocked to my Foo class to use</li>
<li>And finally we raise that last event</li>
<li>And do a simple Boolean check to see actually we did raise the event</li>
</ul>
<p>Wasn&#8217;t that easy <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>You may complain that I have used the generalized EventArgs and completed this post. What about custom EventArgs?</p>
<p>No worries, that also takes the same route <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><span style="font-family:Courier New;font-size:10pt;">[<span style="color:#2b91af;">Test</span>]</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:blue;">public</span> <span style="color:blue;">void</span> TestUpdateEventRaised()<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">{<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">MockRepository</span> fooDatabaseMock = <span style="color:blue;">new</span><br />
<span style="color:#2b91af;">MockRepository</span>();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">IFooDatabase</span> </span></p>
<p><span style="font-family:Courier New;font-size:10pt;">  fooDatabase = fooDatabaseMock.DynamicMock&lt;<span style="color:#2b91af;">IFooDatabase</span>&gt;();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    fooDatabase.FooDatabaseUpdateEvent += <span style="color:blue;">null</span>;<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">LastCall</span>.IgnoreArguments();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">IEventRaiser</span> fooEventRaiser = <span style="color:#2b91af;">LastCall</span>.GetEventRaiser();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    fooDatabaseMock.ReplayAll();<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">Foo</span> myFoo = <span style="color:blue;">new</span><br />
<span style="color:#2b91af;">Foo</span>(fooDatabase);<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">    fooEventRaiser.Raise(<span style="color:blue;">this</span>, <span style="color:blue;">new</span><br />
<span style="color:#2b91af;">FooDatabaseEventArgs</span>(<span style="color:#a31515;">&#8220;datbase&#8221;</span>,<span style="color:blue;">true</span>));<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;"><span style="color:#2b91af;">Assert</span>.IsTrue(myFoo.UpdateEventRaised);<br />
</span></p>
<p><span style="font-family:Courier New;font-size:10pt;">}</span></p>
<p>The <i>FooDatabaseEventArgs</i> is our custom EventArgs.</p>
<p>I would recommend anyone reading this post to try out the sample posted below if you really want to understand what is happening <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>You can download the sample <a href="http://cid-8a2653578ab2c83b.skydrive.live.com/self.aspx/Public/MockingEvents.zip">here</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/chakkaradeep.wordpress.com/552/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/chakkaradeep.wordpress.com/552/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chakkaradeep.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chakkaradeep.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chakkaradeep.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chakkaradeep.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chakkaradeep.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chakkaradeep.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chakkaradeep.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chakkaradeep.wordpress.com/552/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chakkaradeep.wordpress.com/552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chakkaradeep.wordpress.com/552/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=552&subd=chakkaradeep&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chakkaradeep.wordpress.com/2008/04/03/mocking-events-with-rhinomocks/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/94894baff370de381988daaa0011ee42?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">Chaks</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/04/040208-2137-mockingeven1.png" medium="image" />

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/04/040208-2137-mockingeven2.png" medium="image" />
	</item>
		<item>
		<title>Revisiting Mock Objects</title>
		<link>http://chakkaradeep.wordpress.com/2008/02/05/revisiting-mock-objects/</link>
		<comments>http://chakkaradeep.wordpress.com/2008/02/05/revisiting-mock-objects/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 09:35:10 +0000</pubDate>
		<dc:creator>Chaks</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://chakkaradeep.wordpress.com/?p=504</guid>
		<description><![CDATA[
My last post on Mock Objects explained briefly about what is Mocking and gave you an example. But that example is not explained and thought let me explain and how Mocking plays its part here.
So, revisiting that example,
[TestFixture]
public class AdminTests
{
    private MockRepository mocks;
    private EduAdmin eduAdmin;
    [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=504&subd=chakkaradeep&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><img src="http://chakkaradeep.files.wordpress.com/2008/01/tdd.png?w=100&#038;h=100" height="100" width="100" /></p>
<p>My <a href="http://chakkaradeep.wordpress.com/2008/01/25/mock-objects/">last post on Mock Objects</a> explained briefly about what is Mocking and gave you an example. But that example is not explained and thought let me explain and how Mocking plays its part here.</p>
<p>So, revisiting that example,</p>
<pre>[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> AdminTests
{
    <span class="kwrd">private</span> MockRepository mocks;
    <span class="kwrd">private</span> EduAdmin eduAdmin;
    <span class="kwrd">private</span> Bl.IBLAdmin mockAdmin;
    <span class="kwrd">private</span> <span class="kwrd">delegate</span> <span class="kwrd">bool</span> ValidRegisterDelegate(Db.Admin entity);

    [SetUp]
    <span class="kwrd">public</span> <span class="kwrd">void</span> Setup()
    {
         mocks= <span class="kwrd">new</span> MockRepository();

         mockAdmin = mocks.DynamicMock&lt;Bl.IBLAdmin&gt;();
    }

    [Test]
    <span class="kwrd">public</span> <span class="kwrd">void</span> Check_Method_Register()
    {
        AdminEntity inputEntity = <span class="kwrd">new</span> AdminEntity();
        inputEntity.AdminId = <span class="str">“admin”</span>;
        inputEntity.AdminPassword = <span class="str">“admin”</span>;

        Db.Admin dbAdmin=<span class="kwrd">new</span> Educator.DataAccess.Admin
        {
            AdminId = <span class="str">“admin”</span>,
            AdminPwd = <span class="str">“admin”</span>
        };

        With.Mocks(mocks).Expecting(() =&gt;
        {
            Expect
                .Call(mockAdmin.Translate(inputEntity))
                .Return(dbAdmin);

            Expect
                .Call(<span class="kwrd">delegate</span> { mockAdmin.Insert(dbAdmin); })
                .Callback((ValidRegisterDelegate)
                               ((entity) =&gt; { <span class="kwrd">return</span> entity.AdminId == <span class="str">“admin”</span> &amp;&amp; entity.AdminPwd == <span class="str">“admin”</span>; }));

        }).Verify(<span class="kwrd">delegate</span>
       {
           eduAdmin = <span class="kwrd">new</span> EduAdmin(mockAdmin);

           eduAdmin.RegisterAdmin(inputEntity);
       });
    }
}</pre>
<p>To explain the above example, consider this diagram</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/02/mock-objects-1.png" alt="mock-objects-1.png" /></p>
<p>(Please consider the line separation, I will come to it later, Ignore the Tests)</p>
<p>So, as the diagram explains, we have,</p>
<p>1) EduAdmin which is our Service Layer (SL)</p>
<p>2) IBLAdmin which is our Business Layer (BL) Interface, and,</p>
<p>3) Database Layer (DL)</p>
<p>The BL interacts with DL to perform database operations and SL sends requests to BL in regard to what needs to be done.</p>
<p>So, what does the above example test function do? Here it is,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/02/flow.png" alt="flow.png" /></p>
<p>Revisiting again the definition of Unit Testing,</p>
<blockquote><p><i>In <a href="http://en.wikipedia.org/wiki/Computer_programming" title="Computer programming">computer programming</a>, <b>unit testing</b> is a procedure used to validate that individual units of <a href="http://en.wikipedia.org/wiki/Source_code" title="Source code">source code</a></i> are working properly. A unit is the smallest testable part of an application.</p>
<p><i>The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. </i></p></blockquote>
<p>So, testing this <i>RegisterAdmin</i> function involves the use of BL as well DL (via the BL). And also from the first diagram, we do show that each layer is tested against their own tests. So, using Mock Objects, now we come down to,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/02/mock-objects.png" alt="mock-objects.png" /></p>
<p>Since we have mocked our BL, we can now write expectations on how it has to behave and what results it has to give for our Service Layer to work properly <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To dig deep, we may have something like this,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/02/bl-sl.png" alt="bl-sl.png" /></p>
<p><i>Admin</i> is a class that implements our Interface <i>IBLAdmin</i> and <i>AdminEntity </i>is business type and <i>DB.Admin</i> is a database type.With Mocking, we don&#8217;t care about the Class but the Interface. <i>EduAdmin </i>is our Service Layer class and it has a function <i>RegisterAdmin</i></p>
<p>So, to <i>RegisterAdmin </i>function to work properly, we have to see that the BL&#8217;s <i>Translate </i>and <i>Insert </i>functions return proper results and those are nothing but setting expectations on our mock object which is <i>IBLAdmin </i> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> . If you think slowly where we are, you will probably get what is Mocking <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Here is that part which sets expectations and tells what should be returned respectively</p>
<pre>        With.Mocks(mocks).Expecting(() =&gt;
        {
            Expect
                .Call(mockAdmin.Translate(inputEntity))
                .Return(dbAdmin);

            Expect
                .Call(<span class="kwrd">delegate</span> { mockAdmin.Insert(dbAdmin); })
                .Callback((ValidRegisterDelegate)
                               ((entity) =&gt; { <span class="kwrd">return</span> entity.AdminId == <span class="str">“admin”</span> &amp;&amp; entity.AdminPwd == <span class="str">“admin”</span>; }));

        }).Verify(<span class="kwrd">delegate</span>
       {
           eduAdmin = <span class="kwrd">new</span> EduAdmin(mockAdmin);

           eduAdmin.RegisterAdmin(inputEntity);
       });</pre>
<p>What does that <i>ValidRegisterDelegate </i>do? It checks whether the input parameters that is being used to invoke <i>Insert </i>function is of expected type and has expected values. Its more of a <i>RhinoMocks </i>feature, so once you start using it, you will get used to it <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  . And <i>RhinoMocks </i>follows the <i>Record and ReplayAll</i> method where we set expectations means that we record something and once we say to verify, it replays all the set expectations when invoked.</p>
<p>The expectations we set are very simple. When the <i>Translate </i>function is called with an input parameter of type <i>AdminEntity</i>, return an object of type <i>Db.Admin</i> . Similarly for Insert function, except that we don&#8217;t return anything.</p>
<p>In the Verify stage, we can see that we create a new instance of our SL by passing the BL Interface object. What are we doing here? Welcome to the world of <a href="http://en.wikipedia.org/wiki/Dependency_Injection" target="_blank"><i>Dependency Injection</i></a> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  . We explicitly tell that &#8211; &#8220;<i>Hey, here is my BL object which you have to use and please use this</i>&#8220;. We inject dependency here! So, now the BL object is already in the MockRepository and when we tell our SL to use this object, it means that its going to use the MockRepository object with expectations set on it <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> </p>
<p>I think now things are getting clear and you should be able to understand this diagram way better than earlier <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (which is also shown above)</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/02/mock-objects.png" alt="mock-objects.png" /></p>
<p>Coming back to Dependency Injection &#8211; You may be wondering that so if  my client uses this SL, will he always have to explicitly tell  that this is my BL and use it? &#8211; NO. The way we overcome is using multiple constructors <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> EduAdmin
{
     private IBLAdmin blAdmin;

     public EduAdmin(IBLAdmin mockAdmin)
     { blAdmin=mockAdmin;}

      public EduAdmin() : this(new Admin())
     { }
}</pre>
<p>I think thats self explanatory on what we do <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Hope this explained how we Mock <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> </p>
<p>If you have any doubts, please do leave your comments and I will try to answer them <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/chakkaradeep.wordpress.com/504/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/chakkaradeep.wordpress.com/504/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chakkaradeep.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chakkaradeep.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chakkaradeep.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chakkaradeep.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chakkaradeep.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chakkaradeep.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chakkaradeep.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chakkaradeep.wordpress.com/504/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chakkaradeep.wordpress.com/504/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chakkaradeep.wordpress.com/504/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=504&subd=chakkaradeep&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chakkaradeep.wordpress.com/2008/02/05/revisiting-mock-objects/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/94894baff370de381988daaa0011ee42?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">Chaks</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/01/tdd.png" medium="image" />

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/02/mock-objects-1.png" medium="image">
			<media:title type="html">mock-objects-1.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/02/flow.png" medium="image">
			<media:title type="html">flow.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/02/mock-objects.png" medium="image">
			<media:title type="html">mock-objects.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/02/bl-sl.png" medium="image">
			<media:title type="html">bl-sl.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/02/mock-objects.png" medium="image">
			<media:title type="html">mock-objects.png</media:title>
		</media:content>
	</item>
		<item>
		<title>Mock Objects</title>
		<link>http://chakkaradeep.wordpress.com/2008/01/25/mock-objects/</link>
		<comments>http://chakkaradeep.wordpress.com/2008/01/25/mock-objects/#comments</comments>
		<pubDate>Fri, 25 Jan 2008 12:46:01 +0000</pubDate>
		<dc:creator>Chaks</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://chakkaradeep.wordpress.com/?p=484</guid>
		<description><![CDATA[
So, you have started reading or doing more on Test Driven Development? Then am sure you would have now heard about Mocking or Mock Objects  
Before getting started and seeing what is this Mock Object, let me quote the definition of Unit Test from Wikipedia
In computer programming, unit testing is a procedure used to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=484&subd=chakkaradeep&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><img src="http://chakkaradeep.files.wordpress.com/2008/01/tdd.png" alt="tdd.png" /></p>
<p>So, you have started reading or doing more on Test Driven Development? Then am sure you would have now heard about Mocking or Mock Objects <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Before getting started and seeing what is this Mock Object, let me quote the definition of <a href="http://en.wikipedia.org/wiki/Unit_Test" target="_blank">Unit Test from Wikipedia</a></p>
<blockquote><p><i>In <a href="http://en.wikipedia.org/wiki/Computer_programming" title="Computer programming">computer programming</a>, <b>unit testing</b> is a procedure used to validate that individual units of <a href="http://en.wikipedia.org/wiki/Source_code" title="Source code">source code</a></i> are working properly. A unit is the smallest testable part of an application.</p>
<p><i>The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. </i></p></blockquote>
<p>So, Unit Testing covers testing each individual part in your program. In other words, when we say we are unit testing a <i>Component A</i>, we  actually test the functions, properties etc., inside that component.</p>
<p>Below is a pictorial representation of what we do,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/01/unit-test.png" alt="unit-test.png" /></p>
<p>For Component A , we write several unit test cases and test it. Since this component does not depend on anything else, it is fairly easy to test all the parts of the component and hence it satisfies the definition of Unit Testing.</p>
<p>Now we bring in another <i>Component B</i>, which depends on <i>Component A<b> </b></i>and thus now we have something like this,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/01/unit-test-2.png" alt="unit-test-2.png" /></p>
<p>How do we test Component B now?</p>
<p>Let me write down the scenario of where we are now,</p>
<p>1) We have Component A which is been tested</p>
<p>2) We have Component B which uses/depends on some of the functions of Component A</p>
<p>3) We have to test Component B such a way that its isolated from Component A, yet still use Component A</p>
<p>4) If we test along with Component A with Component B, its not Unit Test, rather its, Integration Testing!</p>
<p>What do we do now? Mock Component A <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> </p>
<p>Yes, here comes the use of Mock Objects. From <a href="http://en.wikipedia.org/wiki/Mock_object" target="_blank">Wikipedia</a>,</p>
<blockquote><p><i>In <a href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object-oriented programming">object-oriented programming</a>, <b>mock objects</b> are simulated objects that mimic the behavior of real objects in controlled ways. A <a href="http://en.wikipedia.org/wiki/Computer_programming" title="Computer programming">computer programmer</a> typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a <a href="http://en.wikipedia.org/wiki/Crash_test_dummy" title="Crash test dummy">crash test dummy</a> to test the behavior of a car during an accident. </i></p></blockquote>
<p>So, its kinda Fake Object, but a Mock Object has some expectations and results being set on it.</p>
<p>Now, to make use of Mock Objects, we change the Component A to implement an interface <i>Interface A</i> and Component B an interface <i>Interface B</i> <i>.</i>Using Interfaces helps us to have just the functions and properties of the Component being written which is what Mock needs <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Here we are now,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/01/unit-test-3.png" alt="unit-test-3.png" /></p>
<p>TDD has hence allowed to make changes to your design now and you would have changed tests accordingly.</p>
<p>Introducing Mock Objects, we now have,</p>
<p><img src="http://chakkaradeep.files.wordpress.com/2008/01/unit-test-mocking.png" alt="unit-test-mocking.png" /></p>
<p>So, we interact with Component A using our <i>Mock Repository</i> which holds the <i>Mock Object &#8211; Interface A </i></p>
<p>We can now set expectations on Mock Objects, meaning that, you can say that &#8211; <i>When a function named FunctionA is called, return the string &#8220;Hello World&#8221;</i></p>
<p>It may confuse you initially, but am sure you will get the whole picture once you start mocking yourself <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Here is a sample test case along with Mocking (Using <a href="http://nunit.org/index.php" target="_blank">NUnit</a> and <a href="http://www.ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino Mocks</a>),</p>
<pre>[TestFixture]
<span class="kwrd">public</span> <span class="kwrd">class</span> AdminTests
{
    <span class="kwrd">private</span> MockRepository mocks;
    <span class="kwrd">private</span> EduAdmin eduAdmin;
    <span class="kwrd">private</span> Bl.IBLAdmin mockAdmin;
    <span class="kwrd">private</span> <span class="kwrd">delegate</span> <span class="kwrd">bool</span> ValidRegisterDelegate(Db.Admin entity);

    [SetUp]
    <span class="kwrd">public</span> <span class="kwrd">void</span> Setup()
    {
         mocks= <span class="kwrd">new</span> MockRepository();

         mockAdmin = mocks.DynamicMock&lt;Bl.IBLAdmin&gt;();
    }

    [Test]
    <span class="kwrd">public</span> <span class="kwrd">void</span> Check_Method_Register()
    {
        AdminEntity inputEntity = <span class="kwrd">new</span> AdminEntity();
        inputEntity.AdminId = <span class="str">"admin"</span>;
        inputEntity.AdminPassword = <span class="str">"admin"</span>;

        Db.Admin dbAdmin=<span class="kwrd">new</span> Educator.DataAccess.Admin
        {
            AdminId = <span class="str">"admin"</span>,
            AdminPwd = <span class="str">"admin"</span>
        };

        With.Mocks(mocks).Expecting(() =&gt;
        {
            Expect
                .Call(mockAdmin.Translate(inputEntity))
                .Return(dbAdmin);

            Expect
                .Call(<span class="kwrd">delegate</span> { mockAdmin.Insert(dbAdmin); })
                .Callback((ValidRegisterDelegate)
                               ((entity) =&gt; { <span class="kwrd">return</span> entity.AdminId == <span class="str">"admin"</span> &amp;&amp; entity.AdminPwd == <span class="str">"admin"</span>; }));

        }).Verify(<span class="kwrd">delegate</span>
       {
           eduAdmin = <span class="kwrd">new</span> EduAdmin(mockAdmin);

           eduAdmin.RegisterAdmin(inputEntity);
       });
    }
}</pre>
<p>More resources on Mock Objects,</p>
<p>1) <a href="http://www.mockobjects.com/" target="_blank">All about Mock Objects</a></p>
<p>2) <a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank">Mock aren&#8217;t Stubs</a></p>
<p>3) <a href="http://www.ayende.com/projects/rhino-mocks.aspx" target="_blank">Rhino Mocks</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/chakkaradeep.wordpress.com/484/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/chakkaradeep.wordpress.com/484/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chakkaradeep.wordpress.com/484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chakkaradeep.wordpress.com/484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chakkaradeep.wordpress.com/484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chakkaradeep.wordpress.com/484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chakkaradeep.wordpress.com/484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chakkaradeep.wordpress.com/484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chakkaradeep.wordpress.com/484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chakkaradeep.wordpress.com/484/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chakkaradeep.wordpress.com/484/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chakkaradeep.wordpress.com/484/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=484&subd=chakkaradeep&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chakkaradeep.wordpress.com/2008/01/25/mock-objects/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/94894baff370de381988daaa0011ee42?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">Chaks</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/01/tdd.png" medium="image">
			<media:title type="html">tdd.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/01/unit-test.png" medium="image">
			<media:title type="html">unit-test.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/01/unit-test-2.png" medium="image">
			<media:title type="html">unit-test-2.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/01/unit-test-3.png" medium="image">
			<media:title type="html">unit-test-3.png</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/01/unit-test-mocking.png" medium="image">
			<media:title type="html">unit-test-mocking.png</media:title>
		</media:content>
	</item>
		<item>
		<title>Unit Testing the Database Access Layer</title>
		<link>http://chakkaradeep.wordpress.com/2008/01/11/unit-testing-the-database-access-layer/</link>
		<comments>http://chakkaradeep.wordpress.com/2008/01/11/unit-testing-the-database-access-layer/#comments</comments>
		<pubDate>Fri, 11 Jan 2008 07:47:25 +0000</pubDate>
		<dc:creator>Chaks</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://chakkaradeep.wordpress.com/2008/01/11/unit-testing-the-database-access-layer/</guid>
		<description><![CDATA[
Its been fun after getting into Test Driven Development  
What I do is, test each individual components, layers in my project before its being used by other components and layers. So, currently am testing the Database Access Layer (DAL). Testing DAL is more tricky and many people follow different methods.
1) Many follow a Repository [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=470&subd=chakkaradeep&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><img src="http://chakkaradeep.files.wordpress.com/2008/01/tdd.png" alt="tdd.png" /></p>
<p>Its been fun after getting into <a href="http://chakkaradeep.wordpress.com/2008/01/07/i-am-tdd/" target="_blank">Test Driven Development</a> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>What I do is, test each individual components, layers in my project before its being used by other components and layers. So, currently am testing the Database Access Layer (DAL). Testing DAL is more tricky and many people follow different methods.</p>
<p>1) Many follow a <a href="http://www.martinfowler.com/eaaCatalog/repository.html" target="_blank">Repository Pattern</a> to test the DAL</p>
<p>2) Many use a <a href="http://en.wikipedia.org/wiki/In-memory_database" target="_blank">in-memory database</a> to test the DAL</p>
<p>3) Many use development database to test the DAL</p>
<p>4) Many use live database to test the DAL</p>
<p>So, which is best? &#8211; That really depends on your interest. For the Repository Pattern and in-memory database you may need to write a full length code which does similar job as your DAL, and that requires some amount of your development time. Development database means that each developer will have access to a development database in his local machine or network server. This is also good because you test as what would happen in the real time. Live database is same as development database, except that you are in high risk as you are working against the live database!</p>
<p>What I have chosen? &#8211; Testing against a development database <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>As I learn more about TDD, I will eventually shift to Repository Pattern</p>
<p>I ran into more problems of implementing the Repository Pattern as my DAL is LINQ-to-SQL and though there were few posts about how to unit-test LINQ-to-SQL layer, It was ending up doing more work for testing!</p>
<p>So, what I did was &#8211; I maintained my table data as Lists and passed on to DAL and tested against them.</p>
<p>1) I made my test data ready</p>
<pre><span class="kwrd">private</span> <span class="kwrd">static</span> List&lt;Admin&gt; expectedAdminList;

<span class="kwrd">private</span> <span class="kwrd">void</span> PrepareAdminTestData()
{
    expectedAdminList.Add(<span class="kwrd">new</span> Admin{AdminId = <span class="str">"admin"</span>,AdminPwd = <span class="str">"genius"</span>});
}</pre>
<p>The data that resides in my list is the data that is already present or in other words, the data which I have put in my development database.</p>
<p>2) So, now running a simple test to get the record count,</p>
<pre>[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Check_Property_AdminCount()
{
    <span class="kwrd">int</span> nExpected = expectedAdminList.Count;

    AdminContractor adminContract = <span class="kwrd">new</span> AdminContractor();

    <span class="kwrd">int</span> nResult = adminContract.AdminCount;

    Assert.That(nResult, Is.EqualTo(nExpected));
}</pre>
<p>Its nothing but, checking against the count of my List <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>3) What about Insert?</p>
<pre>[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Check_Method_InsertAdmin()
{
    expectedAdminList.Add(<span class="kwrd">new</span> Admin { AdminId = <span class="str">"chaks"</span>, AdminPwd = <span class="str">"chaks123"</span> });

    Admin inputAdmin = expectedAdminList[(expectedAdminList.Count-1)];

    AdminContractor adminContract = <span class="kwrd">new</span> AdminContractor();

    adminContract.InsertAdmin(inputAdmin);

    Admin retAdmin = adminContract.GetAdmin(inputAdmin.AdminId);

    Assert.That(retAdmin.AdminId, Is.EqualTo(inputAdmin.AdminId));

    Assert.That(retAdmin.AdminPwd, Is.EqualTo(inputAdmin.AdminPwd));
}</pre>
<p>Add to the list and then insert that new element. And now since your List which is holding the table data temporarily is updated, even the above test to count the number of records also passes, as we test the record count against the list count <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Update and Delete also hold the similar way &#8211; Change the list, send to DAL</p>
<p>You have to keep one thing in mind when Deleting &#8211; you have to delete the item from the list too after being successfully removed from the database</p>
<p>Here it is,</p>
<pre>[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Check_Method_DeleteAdmin()
{
    <span class="kwrd">bool</span> blnExpected = <span class="kwrd">true</span>;
    Admin inputAdmin = expectedAdminList[(expectedAdminList.Count-1)];
    <span class="kwrd">string</span> inputAdminId = inputAdmin.AdminId;

    AdminContractor adminContract = <span class="kwrd">new</span> AdminContractor();

    adminContract.DeleteAdmin(inputAdminId);

    <span class="kwrd">bool</span> blnResult = adminContract.CheckAdminIdAvailability(inputAdminId);

    Assert.That(blnResult, Is.EqualTo(blnExpected));

    expectedAdminList.Remove(inputAdmin);
}</pre>
<p>You can see that if the Assert passes,we delete that item from our list.</p>
<p>Remember one thing that if you want to run this test individually (of course we do want it <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  ), use the <b>[Setup]</b> attribute (NUnit)</p>
<p>Here are my test results,</p>
<pre><span class="kwrd">&lt;</span><span class="html">Educator.Tests</span><span class="kwrd">&gt;</span> (32 tests), [0:03.88] Success
  Educator.Tests.BusinessLogicTests (32 tests), [0:03.88] Success
    AdminContractTest (21 tests), [0:03.71] Success
      Check_AdminContract_Default_Contructor, [0:00.01] Success
      Check_Property_AdminCount, [0:01.99] Success
      Check_Method_GetAllAdmins, [0:00.14] Success
      Check_Method_GetAdmin_By_Id_Parameter, [0:00.32] Success
      Check_Method_GetAdmin_By_Name_Parameter_Exception, [0:00.03] Success
      Check_Method_InsertAdmin, [0:00.23] Success
      Check_Method_InsertAdmin_Exception, [0:00.15] Success
      Check_Method_UpdateAdmin, [0:00.10] Success
      Check_Method_CheckAdminIdAvailability_True, [0:00.01] Success
      Check_Method_CheckAdminIdAvailability_False, [0:00.00] Success
      Check_Method_DeleteAdmin, [0:00.03] Success
      Check_Property_DepartmentCount, [0:00.01] Success
      Check_Method_GetAllDepartments, [0:00.01] Success
      Check_Method_GetDepartment_By_Id_Parameter, [0:00.04] Success
      Check_Method_GetDepartment_By_GUID, [0:00.10] Success
      Check_Method_InsertDepartment, [0:00.04] Success
      Check_Method_InsertDepartment_Exception, [0:00.01] Success
      Check_Method_UpdateDepartment, [0:00.03] Success
      Check_Method_CheckDepartmentIdAvailability_True, [0:00.01] Success
      Check_Method_CheckDepartmentIdAvailability_False, [0:00.01] Success
      Check_Method_DeleteDepartment, [0:00.01] Success
    LoginContractTest (11 tests), [0:00.17] Success
      Check_LoginContractor_Default_Constructor, [0:00.01] Success
      Check_Method_CheckIdAvailability_Return_False, [0:00.03] Success
      Check_Method_CheckIdAvailability_Return_True, [0:00.00] Success
      Check_Property_UserCount, [0:00.00] Success
      Check_Method_GetAllUsers, [0:00.01] Success
      Check_Method_GetUser, [0:00.00] Success
      Check_Mthod_Login_True, [0:00.01] Success
      Check_Mthod_Login_False, [0:00.01] Success
      Check_Method_Register, [0:00.01] Success
      Check_Method_UpdateUser, [0:00.01] Success
      Check_Method_DeleteUser, [0:00.03] Success</pre>
<p>I am sure this is not the best way, but it works for now <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/chakkaradeep.wordpress.com/470/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/chakkaradeep.wordpress.com/470/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chakkaradeep.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chakkaradeep.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chakkaradeep.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chakkaradeep.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chakkaradeep.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chakkaradeep.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chakkaradeep.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chakkaradeep.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chakkaradeep.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chakkaradeep.wordpress.com/470/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=470&subd=chakkaradeep&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chakkaradeep.wordpress.com/2008/01/11/unit-testing-the-database-access-layer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/94894baff370de381988daaa0011ee42?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">Chaks</media:title>
		</media:content>

		<media:content url="http://chakkaradeep.files.wordpress.com/2008/01/tdd.png" medium="image">
			<media:title type="html">tdd.png</media:title>
		</media:content>
	</item>
		<item>
		<title>I am TDD :)</title>
		<link>http://chakkaradeep.wordpress.com/2008/01/07/i-am-tdd/</link>
		<comments>http://chakkaradeep.wordpress.com/2008/01/07/i-am-tdd/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 05:00:26 +0000</pubDate>
		<dc:creator>Chaks</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://chakkaradeep.wordpress.com/2008/01/07/i-am-tdd/</guid>
		<description><![CDATA[
TDD isn&#8217;t really a new phrase. Its the acronym of Test Driven Development  
I was introduced to TDD by my friend Ivan and now I firmly believe that TDD is the right development technique and practice that every developer should acquire.
Normally, we are taught the general development principle &#8211; &#8220;Design, Code, Test, Release (DCTR)&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=467&subd=chakkaradeep&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><img src="http://testdriven.com/programs/logo100.png" /></p>
<p>TDD isn&#8217;t really a new phrase. Its the acronym of <a href="http://en.wikipedia.org/wiki/Test_driven_development" target="_blank">Test Driven Development</a> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I was introduced to TDD by my friend <a href="http://www.flanders.co.nz/blog/" target="_blank">Ivan</a> and now I firmly believe that TDD is the right development technique and practice that every developer should acquire.</p>
<p>Normally, we are taught the general development principle &#8211; &#8220;<b>Design, Code, Test, Release </b>(DCTR)&#8221; &#8211; This is the Model which most of the developers/companies follow. But with TDD it becomes &#8211; &#8220;<b>Test, Code, Design</b>&#8220;. What is the big difference here? Yes, there is. With the <i>DCTR</i> approach, your whole development process depends on the design and you strictly follow the design to release the product. The problem comes when there is a need to alter the design and change the code. It becomes very difficult here as your code and design are tightly coupled to each other. With <i>TDD</i>, you write tests for what your application has to do, which ultimately will lead you designing your application.</p>
<p>From <a href="http://en.wikipedia.org/wiki/Test_driven_development" target="_blank">Wikipedia</a>,</p>
<blockquote><p><b>Test-Driven Development</b> (TDD) is a <a href="http://en.wikipedia.org/wiki/Software_development" target="_blank">software development</a> technique consisting of short iterations where new <a href="http://en.wikipedia.org/wiki/Test_case" target="_blank">test cases</a> covering the desired improvement or new functionality are written first, then the production code necessary to pass the tests is implemented, and finally the software is <a href="http://en.wikipedia.org/wiki/Refactor" target="_blank">refactored</a> to accommodate changes. The availability of tests before actual development ensures rapid feedback after any change. Practitioners emphasize that test-driven development is a <a href="http://en.wikipedia.org/wiki/Software_engineering" target="_blank">method of designing software</a>, not merely a method of testing.</p></blockquote>
<p>Many now may have this feeling &#8211; &#8220;<b>I am a Developer and why should I write test cases and test it! Let the Tester do it</b>&#8221; &#8211; Its quite natural to get this feeling <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>But the TDD approach ensures that the code a developer writes is proper, well structured and <b><u>BUG FREE</u></b>. The only way to achieve this is to have Unit Test run on the code we write to see whether we are getting the right job done. If the tests fail, you can easily debug it and make it pass <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Here is an example (I am using <a href="http://nunit.com/index.php" target="_blank">NUnit</a> for Test Framework)</p>
<p>- Lets say you want to write a function to add two numbers. It takes two integers and returns the result back to you.</p>
<p>With the TDD approach we first write a test for this</p>
<pre><span class="kwrd">namespace</span> SampleTests
{
    [TestFixture]
    <span class="kwrd">public</span> <span class="kwrd">class</span> SimpleTests
    {
        [Test]
        <span class="kwrd">public</span> <span class="kwrd">void</span> Check_Method_Add_Return_Result()
        {
            Test myTest = <span class="kwrd">new</span> Test();

            <span class="kwrd">int</span> nExpectedResult = 4;
            <span class="kwrd">int</span> nResult;

            nResult = myTest.Add(2, 2);

            Assert.That(nResult, Is.EqualTo(nExpectedResult));
        }
    }
}</pre>
<p>It looks fairly simple, isn&#8217;t it <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . There isn&#8217;t any magic going. I have an expected result and the result variable. I am going to call the Add function and get the result. If the expected result and the result which Add function gave are same, then it has added it properly <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>So, now you go write your Add function and run the test case to see if your test succeeds</p>
<pre><span class="kwrd">public</span> <span class="kwrd">class</span> Test
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Add(<span class="kwrd">int</span> n1, <span class="kwrd">int</span> n2)
    {
        <span class="kwrd">return</span> (n1 + n2);
    }
}</pre>
<p>This is very very simple example , but I do think its a good place to start. I would suggest writing small programs first and then slowly incorporate the TDD style in your projects.</p>
<p>Some of the tools (for .NET) you might need are,</p>
<p>1) <a href="http://nunit.com/index.php" target="_blank">NUnit</a></p>
<p>2) <a href="http://www.testdriven.net/" target="_blank">TestDriven.net</a></p>
<p>3) <a href="http://www.ayende.com/projects/rhino-mocks.aspx" target="_blank">RhinoMocks</a></p>
<p>4) <a href="http://jetbrains.com/resharper" target="_blank">Resharper</a></p>
<p>And last but not the least, a good read at <a href="http://martinfowler.com/articles.html" target="_blank">Martin Fowler&#8217;s articles</a> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>In the coming days, I would go further into TDD</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/chakkaradeep.wordpress.com/467/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/chakkaradeep.wordpress.com/467/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chakkaradeep.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chakkaradeep.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chakkaradeep.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chakkaradeep.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chakkaradeep.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chakkaradeep.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chakkaradeep.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chakkaradeep.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chakkaradeep.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chakkaradeep.wordpress.com/467/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chakkaradeep.wordpress.com&blog=428358&post=467&subd=chakkaradeep&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://chakkaradeep.wordpress.com/2008/01/07/i-am-tdd/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/94894baff370de381988daaa0011ee42?s=96&#38;d=monsterid&#38;r=G" medium="image">
			<media:title type="html">Chaks</media:title>
		</media:content>

		<media:content url="http://testdriven.com/programs/logo100.png" medium="image" />
	</item>
	</channel>
</rss>