<?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>Vadim's Weblog &#187; SystemWrapper</title>
	<atom:link href="http://vkreynin.wordpress.com/category/systemwrapper/feed/" rel="self" type="application/rss+xml" />
	<link>http://vkreynin.wordpress.com</link>
	<description>Never stop learning.</description>
	<lastBuildDate>Wed, 26 Aug 2009 11:54:05 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='vkreynin.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/1173064e3df3d2dd6e336993810ee5b5?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Vadim's Weblog &#187; SystemWrapper</title>
		<link>http://vkreynin.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vkreynin.wordpress.com/osd.xml" title="Vadim&#8217;s Weblog" />
		<item>
		<title>Tutorial to TDD using Rhino Mocks and SystemWrapper.</title>
		<link>http://vkreynin.wordpress.com/2008/12/04/tutorial-to-tdd-using-rhino-mocks-and-systemwrapper/</link>
		<comments>http://vkreynin.wordpress.com/2008/12/04/tutorial-to-tdd-using-rhino-mocks-and-systemwrapper/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 21:01:41 +0000</pubDate>
		<dc:creator>Vadim</dc:creator>
				<category><![CDATA[Rhino.Mocks]]></category>
		<category><![CDATA[SystemWrapper]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://vkreynin.wordpress.com/2008/12/04/tutorial-to-tdd-using-rhino-mocks-and-systemwrapper/</guid>
		<description><![CDATA[In this post I want to introduce to SystemWrapper and demonstrate how it works with Rhino Mocks.
&#160;
SystemWrapper is just a wrapper to .NET system resources like FileInfo, DirectoryInfo, Assembly, &#8230; .
&#160;
Why do we need SystemWrapper?
&#160;
Let assume that you&#8217;re writing code that creates a directory.&#160; In your unit test you don&#8217;t want to create a directory [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkreynin.wordpress.com&blog=1150196&post=205&subd=vkreynin&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div class="csharpcode"><font size="2">In this post I want to introduce to </font><a href="http://www.codeplex.com/SystemWrapper"><font size="2">SystemWrapper</font></a><font size="2"> and demonstrate how it works with Rhino Mocks.</font></div>
<div class="csharpcode"><font size="2">&nbsp;</font></div>
<div class="csharpcode"><font size="2">SystemWrapper is just a wrapper to .NET system resources like FileInfo, DirectoryInfo, Assembly, &#8230; .</font></div>
<div class="csharpcode">&nbsp;</div>
<h3>Why do we need SystemWrapper?</h3>
<div class="csharpcode"><strong></strong>&nbsp;</div>
<div class="csharpcode"><font size="2">Let assume that you&#8217;re writing code that creates a directory.&nbsp; In your unit test you don&#8217;t want to create a directory you just want to know that Directory.CreateDirectory(@&#8221;C:\MyDir&#8221;) was called.&nbsp; You cannot mock Directory because it&#8217;s a static class.&nbsp; By wrapping Directory in SystemWrapper we can mock Directory object.&nbsp; Whenever you use Directory, just use IDirectoryWrap interface instead.</font></div>
<div class="csharpcode">&nbsp;</div>
<div class="csharpcode"><strong></strong>&nbsp;</div>
<h3>TDD example with Rhino Mocks and SystemWrapper.</h3>
<div class="csharpcode">&nbsp;</div>
<h4>Goal:</h4>
<div class="csharpcode"><font size="2">Create a method that tries to create a directory.&nbsp; If the directory already exists, don&#8217;t create it and return false; otherwise, create directory and return true.&nbsp; Sounds simple enough.</font></div>
<div class="csharpcode">&nbsp;</div>
<h4>Requirements:</h4>
<div class="csharpcode"><a title="Rhino.Mocks" href="http://ayende.com/projects/rhino-mocks.aspx"><font size="2">Rhino Mocks</font></a><font size="2"> &#8211; </font><a href="http://ayende.com/projects/rhino-mocks/downloads.aspx"><font size="2">Download</font></a></div>
<div class="csharpcode"><a href="http://www.codeplex.com/SystemWrapper"><font size="2">SystemWrapper</font></a></div>
<div class="csharpcode"><font size="2">Any unit testing framework of your choice.&nbsp; (I&#8217;m using </font><a href="http://www.mbunit.com/Downloads.aspx"><font size="2">MbUnit v3</font></a><font size="2"> which is part of </font><a href="http://www.gallio.org/Downloads.aspx"><font size="2">Gallio</font></a><font size="2">).</font></div>
<div class="csharpcode"><font size="2">This example is taking advantage of .NET 3.5.&nbsp; Let me know if you want an example using .NET 2.0.</font></div>
<div class="csharpcode"><font size="2">&nbsp;</font></div>
<div class="csharpcode"><font size="2">Now we are ready to start.</font></div>
<div class="csharpcode"><font size="2">&nbsp;</font></div>
<p><font size="2">1. Create new Class Library project and name it MockTutorial.</font></p>
<div class="csharpcode"><a href="http://vkreynin.files.wordpress.com/2008/12/mocktutorial-project.png"><img style="border-width:0;" height="542" alt="MockTutorial project" src="http://vkreynin.files.wordpress.com/2008/12/mocktutorial-project-thumb.png?w=765&#038;h=542" width="765" border="0"></a> </div>
<p><font size="2">2. Rename Class1 to DirectoryInfoSample so your code looks like this:</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">namespace</span> MockTutorial</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> DirectoryInfoSample</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> }</pre>
</div>
</div>
<div><font face="Verdana" size="2"></font>&nbsp;</div>
<div><font face="Verdana" size="2">3. Add a test class.&nbsp; For simplicity we add our test class in the same file.&nbsp; The name of the test class will be DirectoryInfoSampleTests.</font></div>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> DirectoryInfoSampleTests</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> }</pre>
</div>
</div>
<div class="csharpcode">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;"></div>
</div>
</div>
<div class="csharpcode">&nbsp;</div>
<p><font size="2">4. We need to add references to our unit test framework, Rhino Mocks, and SystemWrapper libraries.</font></p>
<div class="csharpcode">
<pre><a href="http://vkreynin.files.wordpress.com/2008/12/mocktutorial-ref.png"><img style="border-width:0;" height="305" alt="MockTutorial ref" src="http://vkreynin.files.wordpress.com/2008/12/mocktutorial-ref-thumb.png?w=262&#038;h=305" width="262" border="0"></a> </pre>
</div>
<p class="csharpcode"><font face="v" size="2">5. Create our first test.&nbsp; Remember that our method should try to create directory and there two possible cases (directory doesn&#8217;t exist and directory exists). It means that we&#8217;ll need two separate tests.&nbsp; Let&#8217;s create the first test when directory already exists.The signature for our method under test will be&nbsp; public bool TryToCreateDirectory(IDirectoryInfoWrap directory)You also will need to add using statements for your unit testing framework, Rhino Mocks, and SystemWrapper:</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">using</span> SystemWrapper.IO;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">using</span> MbUnit.Framework;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">using</span> Rhino.Mocks;</pre>
</div>
</div>
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;"></div>
</div>
<div class="csharpcode">&nbsp;</div>
<p>&nbsp;<font face="v" size="2">Here comes the test.</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> When_try_to_create_directory_that_already_exists_return_false()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     var directoryInfoStub = MockRepository.GenerateStub&lt;IDirectoryInfoWrap&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     directoryInfoStub.Stub(x =&gt; x.Exists).Return(<span style="color:#0000ff;">true</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     Assert.AreEqual(<span style="color:#0000ff;">false</span>, <span style="color:#0000ff;">new</span> DirectoryInfoSample().TryToCreateDirectory(directoryInfoStub));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> }</pre>
</div>
</div>
<div class="csharpcode">
<div class="csharpcode">
<div>
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;"></div>
</div>
<div><font face="Arial" size="2">Line 4 creates the stub object for DirectoryInfo.</font></div>
<div><font face="Arial" size="2">Line 5 stubs Exists property and return true that means that the directory exists.</font></div>
<div><font face="Arial" size="2">Line 6 verifies that TryToCreateDirectory method returns false that means that directory wasn&#8217;t created because it already exists.</font></div>
</div>
</div>
<div class="csharpcode">
<pre class="csharpcode"><font face="v" size="2">&nbsp;</font></pre>
</div>
<p class="csharpcode"><font size="2">6. If you try to compile, it&#8217;s going to fail because we didn&#8217;t create TryToCreateDirectory method. </font></p>
<p class="csharpcode"><font size="2">Let&#8217;s create the method that is going to satisfy our test.</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> TryToCreateDirectory(IDirectoryInfoWrap directory)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">false</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> }</pre>
</div>
</div>
<div class="csharpcode">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;"></div>
</div>
<div class="csharpcode"><font size="2">You just can see that we wrote minimum code to make our test pass successfully.</font></div>
<div class="csharpcode"><font size="2"></font>&nbsp;</div>
<p><font size="2">7. Next we want to create our second test that expects that directory doesn&#8217;t exists and creates it.</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> When_try_to_create_directory_that_does_not_exist_return_true()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     var directoryInfoStub = MockRepository.GenerateStub&lt;IDirectoryInfoWrap&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     directoryInfoStub.Stub(x =&gt; x.Exists).Return(<span style="color:#0000ff;">false</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     Assert.AreEqual(<span style="color:#0000ff;">true</span>, <span style="color:#0000ff;">new</span> DirectoryInfoSample().TryToCreateDirectory(directoryInfoStub));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> }</pre>
</div>
</div>
<div><font size="2"></font>&nbsp;</div>
<div><font size="2">You can see our second test looks very similar to the first one.&nbsp; The only difference, beside the test name, is that when we stub Exists property we return false instead of true.</font></div>
<p><font size="2">If you try to run the test, it will fail because in our test we expect TryToCreateDirectory method to return true.</font></p>
<p><font size="2">8. It&#8217;s time again to work on our method under test.</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> TryToCreateDirectory(IDirectoryInfoWrap directory)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">if</span> (directory.Exists)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">false</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">true</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> }</pre>
</div>
</div>
<p><font size="2">Now our tests should pass.&nbsp; Wait, but how do we know if the directory was created?</font></p>
<div class="csharpcode"><font size="2">&nbsp;</font></div>
<div class="csharpcode"><font size="2">9. Modify our test to check that Create() method was called.</font></div>
<div class="csharpcode"><font size="2">&nbsp;</font></div>
<div class="csharpcode"><font size="2">We can change our stub to mock and create an expectation for Create. If you&#8217;re confused about Mocks and Stubs, you can read </font><a href="http://martinfowler.com/articles/mocksArentStubs.html"><font size="2">Mocks aren&#8217;t Stubs</font></a><font size="2"> by </font><a href="http://martinfowler.com/"><font size="2">Martin Fowler</font></a><font size="2">.&nbsp; The last we verify that Create was executed:</font></div>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> When_try_to_create_directory_that_does_not_exist_return_true()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     var directoryInfoMock = MockRepository.GenerateMock&lt;IDirectoryInfoWrap&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     directoryInfoMock.Stub(x =&gt; x.Exists).Return(<span style="color:#0000ff;">false</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     directoryInfoMock.Expect(x =&gt; x.Create());</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     Assert.AreEqual(<span style="color:#0000ff;">true</span>, <span style="color:#0000ff;">new</span> DirectoryInfoSample().TryToCreateDirectory(directoryInfoMock));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     directoryInfoMock.VerifyAllExpectations();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> }</pre>
</div>
</div>
<div class="csharpcode">&nbsp;</div>
<p><font size="2">It will work but I prefer to use stubs and call new method in Rhino Mocks AssertWasCalled.&nbsp; Isn&#8217;t it looks cleaner?</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> When_try_to_create_directory_that_does_not_exist_return_true()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     var directoryInfoStub = MockRepository.GenerateStub&lt;IDirectoryInfoWrap&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     directoryInfoStub.Stub(x =&gt; x.Exists).Return(<span style="color:#0000ff;">false</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     Assert.AreEqual(<span style="color:#0000ff;">true</span>, <span style="color:#0000ff;">new</span> DirectoryInfoSample().TryToCreateDirectory(directoryInfoStub));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     directoryInfoStub.AssertWasCalled(x =&gt; x.Create());</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> }</pre>
</div>
</div>
<div><font size="2"></font>&nbsp;</div>
<div><font size="2">If you run our second test that we just modified, it will fail.&nbsp; Of course, we never called Create method.</font></div>
<p><font size="2"></font>&nbsp;</p>
<p><font size="2">10. Call Create() in our TryToCreateDirectory method.</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> TryToCreateDirectory(IDirectoryInfoWrap directory)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">if</span> (directory.Exists)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">false</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     directory.Create();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">true</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> }</pre>
</div>
</div>
<div><font size="2"></font>&nbsp;</div>
<div><font size="2">11. We also can modify our first test to make sure Create() wasn&#8217;t call in case when directory already exists.</font></div>
<p><font size="2">In this case we call Rhino Mocks method AssertWasNotCalled.</font></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> When_try_to_create_directory_that_already_exists_return_false()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     var directoryInfoStub = MockRepository.GenerateStub&lt;IDirectoryInfoWrap&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     directoryInfoStub.Stub(x =&gt; x.Exists).Return(<span style="color:#0000ff;">true</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     Assert.AreEqual(<span style="color:#0000ff;">false</span>, <span style="color:#0000ff;">new</span> DirectoryInfoSample().TryToCreateDirectory(directoryInfoStub));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     directoryInfoStub.AssertWasNotCalled(x =&gt; x.Create());</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> }</pre>
</div>
</div>
<div><font face="v" size="2"></font>&nbsp;</div>
<div><font face="v" size="2">To see all the classes that wrapped by SystemWrapper follow</font> <a href="http://www.codeplex.com/SystemWrapper/Wiki/View.aspx?title=Wrapper%20classes&amp;referringTitle=Home"><font face="v" size="2">this link</font></a><font face="v" size="2">.</font></div>
<div>&nbsp;</div>
<div><font face="v" size="2">More to come.&nbsp; I&#8217;d like to hear from you what classes would you like to add to SystemWrapper first.</font></div>
<div class="csharpcode">&nbsp;</div>
<div class="csharpcode">
<div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:fed77133-8a68-434e-ab09-c6b4b8087afc" style="display:inline;margin:0;padding:0;">Technorati Tags: <a href="http://technorati.com/tags/TDD" rel="tag">TDD</a>,<a href="http://technorati.com/tags/Rhino%20Mocks" rel="tag">Rhino Mocks</a>,<a href="http://technorati.com/tags/Mocks" rel="tag">Mocks</a>,<a href="http://technorati.com/tags/MbUnit" rel="tag">MbUnit</a>,<a href="http://technorati.com/tags/SystemWrapper" rel="tag">SystemWrapper</a></div>
</div>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fvkreynin.wordpress.com%2f2008%2f12%2f04%2ftutorial-to-tdd-using-rhino-mocks-and-systemwrapper%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fvkreynin.wordpress.com%2f2008%2f12%2f04%2ftutorial-to-tdd-using-rhino-mocks-and-systemwrapper%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vkreynin.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vkreynin.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vkreynin.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vkreynin.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vkreynin.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vkreynin.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vkreynin.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vkreynin.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vkreynin.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vkreynin.wordpress.com/205/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vkreynin.wordpress.com&blog=1150196&post=205&subd=vkreynin&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://vkreynin.wordpress.com/2008/12/04/tutorial-to-tdd-using-rhino-mocks-and-systemwrapper/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0bdbc8162336851af022196f897d6200?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vkreynin</media:title>
		</media:content>

		<media:content url="http://vkreynin.files.wordpress.com/2008/12/mocktutorial-project-thumb.png" medium="image">
			<media:title type="html">MockTutorial project</media:title>
		</media:content>

		<media:content url="http://vkreynin.files.wordpress.com/2008/12/mocktutorial-ref-thumb.png" medium="image">
			<media:title type="html">MockTutorial ref</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fvkreynin.wordpress.com%2f2008%2f12%2f04%2ftutorial-to-tdd-using-rhino-mocks-and-systemwrapper%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
	</channel>
</rss>