<?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/"
	>

<channel>
	<title>Elegant Code &#187; TDD</title>
	<atom:link href="http://elegantcode.com/category/tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 20 Jul 2010 12:52:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Boxcar Case Macro in Visual Studio 2010 &#8211; Video</title>
		<link>http://elegantcode.com/2010/05/17/boxcar-case-macro-in-visual-studio-2010-video/</link>
		<comments>http://elegantcode.com/2010/05/17/boxcar-case-macro-in-visual-studio-2010-video/#comments</comments>
		<pubDate>Mon, 17 May 2010 17:18:55 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/05/17/boxcar-case-macro-in-visual-studio-2010-video/</guid>
		<description><![CDATA[I get so much use out of this handy dandy little macro that I just had to share it. It is a simple little Visual Studio macro that should work in 2010 or 2008 to change a string like this:
“When writing a unit test”
into this:
When_writing_a_unit_test
And do it in a single keystroke. My dirty little secret? [...]]]></description>
			<content:encoded><![CDATA[<p>I get so much use out of this handy dandy little macro that I just had to share it. It is a simple little Visual Studio macro that should work in 2010 or 2008 to change a string like this:</p>
<p>“When writing a unit test”</p>
<p>into this:</p>
<p>When_writing_a_unit_test</p>
<p>And do it in a single keystroke. My dirty little secret? I am a bad typist, so I need little helpers like this.</p>
<p>The following video will walk you through installing the macro and binding it up to a keyboard shortcut. If you haven’t worked with Visual Studio macros before, this might help get there a bit faster.</p>
<p><a href="http://pluralsight-free.s3.amazonaws.com/david-starr/videos/boxcar_macro_in_vs_2010.mp4" target="_blank"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2010/05/image1.png" width="244" height="184" /></a> </p>
<p><a href="http://pluralsight-free.s3.amazonaws.com/david-starr/videos/boxcar_macro_in_vs_2010.mp4" target="_blank">MP4</a></p>
<p>&#160;</p>
<p>And here is the macro itself.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> System</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> EnvDTE</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> EnvDTE80</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> EnvDTE90</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> EnvDTE90a</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> EnvDTE100</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> System.Diagnostics</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> System.Windows.Forms</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Imports</span> System.Text.RegularExpressions</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">Public</span> <span style="color: #0000ff">Module</span> BddMethods</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">Sub</span> BoxcarTheCurrentLineOfCode()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Dim</span> description <span style="color: #0000ff">As</span> <span style="color: #0000ff">String</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Dim</span> regEx <span style="color: #0000ff">As</span> Regex = <span style="color: #0000ff">New</span> Regex(<span style="color: #006080">&quot;&quot;</span><span style="color: #006080">&quot;([^&quot;</span><span style="color: #006080">&quot;]+)&quot;</span><span style="color: #006080">&quot;&quot;</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">If</span> DTE.ActiveDocument <span style="color: #0000ff">Is</span> <span style="color: #0000ff">Nothing</span> <span style="color: #0000ff">Then</span> <span style="color: #0000ff">Return</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Dim</span> currentLine <span style="color: #0000ff">As</span> TextSelection = GetCurrentLine()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">If</span> (currentLine <span style="color: #0000ff">Is</span> <span style="color: #0000ff">Nothing</span> <span style="color: #0000ff">Or</span> currentLine.Text.Length() &lt; 1) <span style="color: #0000ff">Then</span> <span style="color: #0000ff">Return</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Dim</span> stringMatch <span style="color: #0000ff">As</span> Match = regEx.Match(currentLine.Text)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">For</span> <span style="color: #0000ff">Each</span> stringMatch <span style="color: #0000ff">In</span> regEx.Matches(currentLine.Text)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            currentLine.Text = currentLine.Text.Replace(stringMatch.Value, BoxcarAString(stringMatch.Value))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Next</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        currentLine.EndOfLine()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Sub</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">Private</span> <span style="color: #0000ff">Function</span> BoxcarAString(<span style="color: #0000ff">ByVal</span> textSegmentToChange <span style="color: #0000ff">As</span> <span style="color: #0000ff">String</span>) <span style="color: #0000ff">As</span> <span style="color: #0000ff">String</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Dim</span> segmentAfterTheChange <span style="color: #0000ff">As</span> <span style="color: #0000ff">String</span> = textSegmentToChange.Trim()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        segmentAfterTheChange = segmentAfterTheChange.Replace(<span style="color: #006080">&quot;&quot;</span><span style="color: #006080">&quot;&quot;</span>, <span style="color: #006080">&quot;&quot;</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        segmentAfterTheChange = segmentAfterTheChange.Replace(<span style="color: #006080">&quot; &quot;</span>, <span style="color: #006080">&quot;_&quot;</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Return</span> segmentAfterTheChange</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">Private</span> <span style="color: #0000ff">Function</span> GetCurrentLine() <span style="color: #0000ff">As</span> TextSelection</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Dim</span> currentLine <span style="color: #0000ff">As</span> TextSelection = DTE.ActiveDocument.Selection()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        currentLine.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        currentLine.EndOfLine(<span style="color: #0000ff">True</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">Return</span> currentLine</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">End</span> <span style="color: #0000ff">Module</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/05/17/boxcar-case-macro-in-visual-studio-2010-video/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
<enclosure url="http://pluralsight-free.s3.amazonaws.com/david-starr/videos/boxcar_macro_in_vs_2010.mp4" length="15001011" type="video/mp4" />
		</item>
		<item>
		<title>An Evolution of Test-Specification Styles &#8211; My Journey to MSpec</title>
		<link>http://elegantcode.com/2010/03/01/an-evolution-of-test-specification-styles-my-journey-to-mspec/</link>
		<comments>http://elegantcode.com/2010/03/01/an-evolution-of-test-specification-styles-my-journey-to-mspec/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 03:32:57 +0000</pubDate>
		<dc:creator>Richard Cirerol</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[MSpec]]></category>
		<category><![CDATA[NUnit]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/01/an-evolution-of-test-specification-styles-my-journey-to-mspec/</guid>
		<description><![CDATA[Over the last few years, the practice of Test-Driven Design and Behavior-Driven Design has increased in acceptance, even if its practice has not increased in equal proportion.&#160; From my perspective, specification-based design is a natural way to develop software.&#160; Each developer has a preferred framework, and a framework they loathe.&#160; In the .NET world, there [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last few years, the practice of Test-Driven Design and Behavior-Driven Design has increased in acceptance, even if its practice has not increased in equal proportion.&#160; From my perspective, specification-based design is a natural way to develop software.&#160; Each developer has a preferred framework, and a framework they loathe.&#160; In the .NET world, there are a plethora of unit testing frameworks &#8211; NUnit has been nearly ubiquitous; mbUnit was evolutionary; xUnit was revolutionary; MSTest is…er, getting an update.&#160; For BDD, you can find frameworks like SpecUnit.NET, NBehave, and MSpec.&#160; My current framework of choice is MSpec, especially when <a href="http://elegantcode.com/2009/11/30/wrapping-up-the-structuremap-automocking-container/" target="_blank">coupled with StructureMap’s automocking container and a couple helper classes</a>. </p>
<p>From my unscientific observation, developers either love or hate MSpec.&#160; For those that hate MSpec, I wonder if it is the grammatical syntax or the lambda-expression style creating the aversion.&#160; Some even expound MSpec as cryptic and cumbersome.&#160; This seems to be a common complaint – one I hope to address in this post.</p>
<p>I find that those drawn to MSpec have experienced an evolution of testing/specification styles, influenced partially by the community and partially by frameworks they have used.&#160; At the very least, this has been my experience as I have transitioned from xUnit frameworks. </p>
<h4>Conventions of MSpec</h4>
<p>I particularly like the conventions MSpec encourages using its four delegate types:</p>
<p><strong>Establish</strong>: </p>
<ul>
<li>Provides setup (context) for the specification </li>
<li>One per class (but not required) </li>
<li>“Unlimited” per specification or inheritance chain </li>
<li>Runs down the inheritance chain prior to the <strong>Because</strong> delegate </li>
</ul>
<p><strong>Because</strong>:</p>
<ul>
<li>The action/event being tested </li>
<li>One per specification </li>
</ul>
<p><strong>It</strong>: </p>
<ul>
<li>An assertion (usually extension methods/fluent interfaces based on NUnit/xUnit Assert classes) </li>
<li>“Unlimited” per specification/class </li>
<li>Can be “hosted” in a <strong>Behavior</strong> class </li>
<li>Runs after the <strong>Because</strong> </li>
<li>Can be reused when placed in a <strong>Behavior</strong> class </li>
<li>If body of delegate is missing, will mark as “Ignored” or “Not Implemented” in a test runner </li>
</ul>
<p><strong>Cleanup</strong>:</p>
<ul>
<li>Provides teardown for the specification </li>
<li>One per class (but not required) </li>
<li>“Unlimited” per specification or inheritance chain </li>
<li>Runs up the inheritance chain after the <strong>It</strong> delegates are completed </li>
</ul>
<p>MSpec also has one other delegate type (<code>Behaves_like&lt;T&gt;</code>) and several optional attributes available (<code>Subject, Tag, Ignore, Behavior</code>).&#160; <code>[Subject]</code> provides additional information about the specification. <code>[Tag]</code> is similar to the <code>[Category]</code> attribute in NUnit.&#160; <code>[Ignore]</code> is self-explanatory. <code>[Behavior]</code> marks a class made up entirely of assertions (<code>It</code> delegates).&#160; <code>Behaves_like&lt;T&gt;</code> is a delegate type that can be included in a specification block taking the place of a set of assertions (<code>It</code> delegates).</p>
<p>&#160;</p>
<h4>NUnit – TestFixture-Per-Class </h4>
<p>I have gone from using a TestFixture-Per-Class style to a behavior-driven/context-specification style.&#160; I would like to use the standard HomeController, created in an ASP.NET MVC 1.0 project, as an example class to explore this evolution.&#160; </p>
<p>In my TestFixture-Per-Class days, I may have tested the <code>Index</code> action as follows:</p>
<pre class="brush: csharp;">[TestFixture]
public class HomeControllerTests
{
    [Test]
    public void IndexActionTest()
    {
        var controller = new HomeController();
        var result = (ViewResult) controller.Index();
        Assert.AreEqual(&quot;Welcome to ASP.NET MVC!&quot;, result.ViewData[&quot;Message&quot;]);
    }
}</pre>
<p>I do like the simplicity of the code.&#160; One line to set up the test, one to execute, and one to assert.&#160; There is a little ceremony involved with marking the class and methods with the NUnit attributes.&#160; With this simple test, it is easy to distinguish the setup code from the assertion; yet, a more complex test would make scanning the test more difficult.&#160; Comments can help, but tend to just add noise.</p>
<p>&#160;</p>
<h4>NUnit – Arrange/Act/Assert</h4>
<p>With the advent of BDD, I started naming my classes and expectations as inspired by BDD syntax.&#160; I might have used the <code>[SetUp]</code> attribute or a base class to organize my test code in a style similar to the following:</p>
<pre class="brush: csharp;">[TestFixture]
public class when_I_go_to_the_home_page: AAA
{
    private HomeController _controller;
    private ViewResult _result;

    protected override void Arrange()
    {
        _controller = new HomeController();
    }
    protected override void Act()
    {
        _result = (ViewResult)_controller.Index();
    }

    [Test]
    public void then_the_welcome_message_should_be_displayed()
    {
        _result.ViewData[&quot;Message&quot;].ShouldEqual(&quot;Welcome to ASP.NET MVC!&quot;);
    }
}</pre>
<p>This actively separates out the different responsibilities of the test.&#160; You can see where the setup code is, as well as the action being tested.&#160; The assertion is the only line of code in the test method.&#160; There is still ceremony involved with the attributes.&#160; Plus, we have added a little more ceremony and noise with the <code>Arrange()</code> and <code>Act()</code> overrides.&#160; This simple test does not necessarily need the parts split in this manner, but it would help with more complex scenarios.&#160; Additionally, the assertion is using extension methods based on the NUnit Assert classes which provides a little more clarity to the assertion.</p>
<p>&#160;</p>
<h4>MSpec</h4>
<p>Once started down the path of a behavior-driven (or context/specification) style, I started looking at frameworks devoted to that style.&#160; MSpec is one of those frameworks.&#160; Here is the Index action specification in MSpec:</p>
<pre class="brush: csharp;">public class when_I_go_to_the_home_page
{
    Establish context = () =&gt;  _controller = new HomeController();
    Because of = () =&gt; _result = (ViewResult)_controller.Index();
    It should_display_the_welcome_message = () =&gt; _result.ViewData[&quot;Message&quot;].ShouldEqual(&quot;Welcome to ASP.NET MVC!&quot;); 

    static HomeController _controller;
    static ViewResult _result;
}</pre>
<p>If you look closely, this specification is very similar to the first NUnit test.&#160; However, there are no attributes to specify and the specification has a distinct functional separation.&#160; The Establish/Because/It syntax is analogous to the Given/When/Then language used in user stories or Arrange/Act/Assert syntaxes used in the previous example.&#160; In my opinion, this has less noise than the test using NUnit with an AAA base class – even while using the lambda delegate syntax.</p>
<p>&#160;</p>
<h4>MSpec &#8211; SpecificationFor&lt;ClassUnderTest&gt;</h4>
<p>I often use a base class to handle some of my setup code. Using StructureMap and RhinoMocks, it generates and injects proxied dependencies for the given class.&#160; In this example, I have no dependencies to wire up, but I think you can still see the benefits.</p>
<pre class="brush: csharp;">public class when_I_go_to_the_home_page : SpecificationFor&lt;HomeController&gt;
{
    Because of = () =&gt; _result = (ViewResult)ClassUnderTest.Index();
    It should_display_the_welcome_message = () =&gt; _result.ViewData[&quot;Message&quot;].ShouldEqual(&quot;Welcome to ASP.NET MVC!&quot;); 

    static ViewResult _result;
}</pre>
<p>The Establish delegate is not needed in this specification, because I have wired up the class via the base class.&#160; I feel this version is most clear and concise of all the examples, and neither cryptic nor cumbersome&#160; If you are interested in the <code>SpecificationFor&lt;T&gt;</code> base class and the supporting components, check out my previous <a href="http://elegantcode.com/2009/11/30/wrapping-up-the-structuremap-automocking-container/" target="_blank">post</a>.&#160; </p>
<p>&#160;</p>
<h4>Looking to the Future</h4>
<p>I hope that this post has shown that MSpec does not have to be cryptic, nor cumbersome.&#160; If you considered it as such before, please take another critical look.</p>
<p>Given the evolving state of the craft, I am sure that my test/specification style will continue to gradually change.&#160; I am sure that my framework of choice will eventually change as well, especially as each framework is extended and challenged by others.&#160; Hopefully, you can be open to evolving your style as well.</p>
<p><script type="text/javascript">// <![CDATA[
 var cirerolGaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + cirerolGaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
// ]]&gt;</script><script type="text/javascript">// <![CDATA[
 var cirerolPageTracker = _gat._getTracker('UA-8257866-3'); cirerolPageTracker._initData(); cirerolPageTracker._trackPageview();
// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/01/an-evolution-of-test-specification-styles-my-journey-to-mspec/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Note to self: Asp.Net MVC Controllers are not Code Behind</title>
		<link>http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/</link>
		<comments>http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 22:28:27 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Asp.Net MVC]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/</guid>
		<description><![CDATA[Sometimes I think things are more complicated than they really are.&#160; I got my head into a bind recently when I had some code that I really wanted under test in a Controller Action.
My first thought was “Crap, this is going to be rough”.&#160; Mainly I was still thinking of my time trying to test [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I think things are more complicated than they really are.&#160; I got my head into a bind recently when I had some code that I really wanted under test in a Controller Action.</p>
<p>My first thought was “Crap, this is going to be rough”.&#160; Mainly I was still thinking of my time trying to test Code Behind in Asp.Net WebForms.&#160; I had to smack myself a few times after I remembered that the Controller base class is an abstract type.&#160; Reality is that a Controller doesn’t have to be any harder to test than any other service class.&#160; It isn’t like inheriting from Page (Asp.Net WebForms).</p>
<p>Not that my code did not have some lingering issues.&#160; Like: why did I have something in my Controller Action that would require testing in the first place?&#160; I hate having much logic at all in my Actions.&#160; Get input, send it off for processing (logic), go somewhere.&#160;&#160; That should be about it – unless you have to go to different places depending on what is returned.&#160; Now, I want some tests.</p>
<p>Anyway, now back to your regularly scheduled program.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Driven Spikes (TDS)</title>
		<link>http://elegantcode.com/2010/01/04/test-driven-spikes-tds/</link>
		<comments>http://elegantcode.com/2010/01/04/test-driven-spikes-tds/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 00:10:09 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/04/test-driven-spikes-tds/</guid>
		<description><![CDATA[I have had a discussion with a college of mine about spike code and how to bring the chosen solution into production code and quality. Jeremy wrote about it a while back stating to never check-in spiked code, and I was wondering if I could challenge that a little bit.
Now guys, don’t look at me [...]]]></description>
			<content:encoded><![CDATA[<p>I have had a discussion with a college of mine about spike code and how to bring the chosen solution into production code and quality. Jeremy <a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/11/11/don-t-check-in-spike-code.aspx" target="_blank">wrote about it a while back</a> stating to never check-in spiked code, and I was wondering if I could challenge that a little bit.</p>
<p>Now guys, don’t look at me in that way, I mean it well …</p>
<h2>What is spike code?</h2>
<p><a href="http://www.extremeprogramming.org/rules/spike.html" target="_blank">A spike solution is a very simple program to explore potential solutions</a>. Well I like to add that I think a spike is relatively small, if it becomes very large I am wondering if we are not talking about prototyping instead? In my experience a spike is perhaps a days worth of coding, but then that it is rather much, more normal is an hour or two. Within this time there could be several attempts to get the desired result.</p>
<h2>There is spike code and there is SPIKE code</h2>
<p>Jeremy is talking about spike code to learn an API or something like that, in that case you don’t have a real use case that you need to solve so there I completely agree and it should be deleted as soon as you have learned what you needed to know (or perhaps checked-in into a special spike area). I would not really consider this writing spike code anyway.</p>
<p>Also code that is really hacked together and not following any good design principles, there adding tests afterwards would not give you anything, if at all possible. Just throw away that code and start over again.</p>
<h2>Test Driven Spikes (TDS)</h2>
<p>First of all sorry for another three letter acronym.</p>
<p>When I write a spike I almost always drive the execution of this code through tests, tests are dead easy to fire-of and they give you instant feedback. It is also very easy to have several tests next to each other exploring different angles or approaches. So while creating my spikes they are sort of driven by tests. And I use tests to verify that my spike code does what I expect from it. It has been a very long time ago that I created a console application to test or try-out some code.</p>
<p>But those are not spikes!</p>
<p>Well what is different here from actual Test Driven <strike>Development</strike> Design (can we just start calling TDD; Test Driven design?) is that I do take shortcuts, not all angles are covered correctly and my tests are not very descriptive and readable. So it is not production ready code. Also the actual code should be cleaned-up, different naming and removing the new keywords if they are any at all.</p>
<p>For example: I would write a specification (or test if you will) that dictates that I want such and so to happen when doing this and that. I do this from a fairly high level, so most likely not directly on the class that does the specific work, but it could be. Anyway while trying to get this to pass I run into a little problem. The solution is not very straightforward and I am stuck. </p>
<p>So for me that is when the spike begins, the existing test remains, but I ignore it for now. I probably create a similar test but now much closer to the actual problem itself. And from there I start playing with the code. </p>
<p>I would also try to take small subsets of the problem and try to fix that independently from the bigger problem area, again using tests. So when&#160; have a few of these smaller subsets working I try to put them together. I refactor the code and tests to get my code working the way I need it to work. </p>
<p>When the spike goes bad I sometimes completely delete all the spike code and start over again, but more often I delete subsets of the spike and redo those. Then when I get the functionality that was needed in the initial specification I hook it up, and verify that it works.</p>
<p>When it does then I go back to the spike code and start cleaning it up as mentioned before, then I could also add some more tests for some edge cases, but more likely is that those edge cases would be discovered by sequential specifications, meaning actual use cases that are needed.</p>
<h2>Also I am a bit weird</h2>
<p>I am really bad at really hacking code together or I am just too stupid, but if I don’t clean-up after myself I lose the bigger picture or overview. Even in spikes, if I have the same code in a few places it annoys me, so I refactor it into a single method or into its own class. The same goes for naming, I don’t use variable names like a, b and c that makes no sense to me, also ReSharper makes it a no brainer anyway. Having unclean code just feels wrong to me.</p>
<p><em>Its the same with other things in my daily live, some stuff just needs to be true, like the car keys need to be on the desk. I can place them somewhere else, but next time I see it I move them to the right place. The volume knob in my car needs to be in a certain position, if it is not I use the volume on my steering wheel to adjust the knob without the volume. So yes I might have issues!</em></p>
<p>I think good design in spike code is important as well because it gives a better overview of what is going on, and if there is a place where you really need that then it is in code that you don’t really understand yet, i.e. spike code.</p>
<p>So in the end my spike code might actually get a much fine-grained test suite then the code that was not spiked. And this might actually make the test suite more brittle around this code, so it is not always a good thing, but for the time being I leave them where they are. Which is in a different place from the actual specifications so they are recognizable as spike tests.</p>
<h2>From memory</h2>
<p><em>Update: This paragraph came to me after having slept for the night.</em></p>
<p>I often hear that when deleting the spike code and starting over again you come-up with a much nicer solution then you finished with in the spike. </p>
<p>Now lets say you work like this and delete the whole spike code and started from scratch, don’t you think that the tests you will write are very much towards the already thought of solution? Can you really disconnect yourself from your previous attempt enough to come-up with better tests and potential better solution? I doubt it. Perhaps this will be true if you indeed really cut al corners, but even then the solution will be to close in memory.</p>
<h2>Conclusion</h2>
<p>So maybe we can conclude that I don’t know how to write spike code? Or that I am doing it all wrong? I would love to hear your opinions about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/04/test-driven-spikes-tds/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unity/Moq &#8211; AutoMocker or AutoMockingContainer</title>
		<link>http://elegantcode.com/2010/01/03/unitymoq-automocker-or-automockingcontainer/</link>
		<comments>http://elegantcode.com/2010/01/03/unitymoq-automocker-or-automockingcontainer/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 16:52:33 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/03/unitymoq-automocker-or-automockingcontainer/</guid>
		<description><![CDATA[What is an Auto Mocking Container?
This post started to get a little long, so I won’t re-explain the concept.
Joshua Flanagan wrote a nice overview at his Los Techies blog: Auto mocking Explained.
My post is mainly here to describe the Unity version of an automocking container I threw together.
In Jan 2009 I blogged about my initial [...]]]></description>
			<content:encoded><![CDATA[<h4>What is an Auto Mocking Container?</h4>
<p>This post started to get a little long, so I won’t re-explain the concept.</p>
<p><a href="http://www.lostechies.com/blogs/joshuaflanagan/default.aspx">Joshua Flanagan</a> wrote a nice overview at his <a href="http://www.lostechies.com/">Los Techies</a> blog: <a href="http://www.lostechies.com/blogs/joshuaflanagan/archive/2009/02/03/auto-mocking-explained.aspx"><strong>Auto mocking Explained</strong></a>.</p>
<p>My post is mainly here to describe the Unity version of an automocking container I threw together.</p>
<h4>In Jan 2009 I blogged about my initial version of the Unity AutoMocker &#8211; Why am I blogging about it again?</h4>
<p>I originally wrote the AutoMocker for the Unity container a year ago (Jan 2009, in Silverlight), and finally got around to placing the code up in the <a href="http://code.google.com/p/moq-contrib/">moq-contrib</a> project in June of 2009.</p>
<p>I’m writing another post today in hopes to:</p>
<ol>
<li>Get some feedback on how this little snippet of code should continue. </li>
<li>Give a little more how-to/example code </li>
<li>Describe some updates I made since I originally created it.&#160; </li>
</ol>
<h4>Where can I get it?</h4>
<p><strong>This is one part where I’d appreciate some feedback.</strong></p>
<p>I have two slightly different versions out there (currently).</p>
<p>I have one version at the <a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/JasonJarrett/UnityAutoMocker">ElegantCode</a> repository where I was working on it, and the other I threw up at <a href="http://code.google.com/p/moq-contrib/">moq-contrib</a>.</p>
<p>The core of the UnityAutoMockContainer is the same in both places, it’s how the tests are separated out that differ.</p>
<p>In the <a href="http://code.google.com/p/elegantcode/source/browse/#svn/trunk/Coders/JasonJarrett/UnityAutoMocker">ElegantCode repository</a> it’s an all in one self contained single file (that you can copy into your own test project(s)). You can then setup a single test in your own testing framework that runs all internal automocker tests (in case you need to modify it yourself, and don’t want to break any existing functionality). EX: test</p>
<pre class="brush: csharp;">[Test]
public void Should_run_all_UnityAutoMockContainer_internal_tests()
{
    Moq.AutoMocking.SelfTesting.UnityAutoMockContainerFixture
        .RunAllTests(Console.WriteLine);
}</pre>
<p>I kind of like this format as it makes it easy to port between test libraries. Can’t say I like having the tests in the same file as the core, but it certainly is not a large chunk of code (so far) so it’s relatively small to maintain and definitely easier to manage as a single .cs file than another assembly (which would have to be version dependent on both Unity and Moq).</p>
<p>The <a href="http://code.google.com/p/moq-contrib/">moq-contrib</a> is definitely where I think this helper should end up (and it is there). I just happened to break the tests out into the Silverlight test project and the core is alone in a file. </p>
<p>It’s current state isn’t as easy to copy to a test library (Silverlight/Desktop/Unity 1.2/Unity 2.0) as the one at <a href="http://elegantcode.com/">ElegantCode</a>. <strong>What does anyone think?</strong> Should I put them all together in one file?</p>
<p>However it ends up (1. all in one file or two (1 test) (1 core)) it will continue to be maintained on the <a href="http://code.google.com/p/moq-contrib/">moq-contrib</a> project.</p>
<h4>What is the high level API of the container?</h4>
<p>It’s pretty simple, (currently) there are four methods on the container.</p>
<p><strong>Two for registering items with the container</strong>. Say you want to register an already created instance, or say you want to map an interface to a concrete class and _not_ have the container generate mocks automatically for special cases. </p>
<p>And <strong>two for pulling items out of the container</strong>. Whether you want a instance of T or a Mock&lt;T&gt;, it gives you ways to retrieve both.</p>
<blockquote>
<p><a href="http://elegantcode.com/wp-content/uploads/2010/01/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2010/01/image_thumb.png" width="396" height="106" /></a> </p>
</blockquote>
<h4>How can I setup my own registrations with the container? </h4>
<p>Say I don’t want to have the container generate mocks for specific items and I want to supply specific configuration to the UnityContainer.</p>
<blockquote>
<p>public UnityAutoMockContainer RegisterInstance&lt;TService&gt;(TService instance)</p>
<p>public UnityAutoMockContainer Register&lt;TService, TImplementation&gt;()<br />
    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where TImplementation : TService</p>
</blockquote>
<p><strong>Note</strong>: both of these registration methods return the container itself so you can fluently stack registration. <strong>EX:</strong></p>
<pre class="brush: csharp;">AutoMockContainer
    .Register&lt;IServiceA, ServiceA&gt;()
    .Register&lt;IServiceB, ServiceB&gt;();</pre>
<p><strong>Let me know</strong>: I haven’t tested or played around with how this automocking container deals with any container specific xml configuration… So although I don’t think you should probably have that in a test assembly (stuff happens). Let me know if there are any issues.</p>
<h4>How do I get items out of the container?</h4>
<p>First is the Resolve&lt;T&gt;(). It will pull an item T out of the container. (Creating it if not already existing)</p>
<blockquote>
<p>public T Resolve&lt;T&gt;()</p>
</blockquote>
<p>When T is an interface Resolve&lt;T&gt; (unless you setup registration specifically with the container) should return basically “(new Mock&lt;T&gt;()).Object”</p>
<p>When T is a concrete Class, the container should return an instance of T and any of it’s dependencies will be satisfied by mocks. (Note that T will not be any sort of mocked instance of T, unless you used the GetMock&lt;T&gt; as described below first)</p>
<blockquote>
<p>public Mock&lt;T&gt; GetMock&lt;T&gt;() where T : class</p>
</blockquote>
<p>When T is an interface GetMock&lt;T&gt; should return basically “(new Mock&lt;T&gt;())” </p>
<p>When T is a concrete Class, the container should return a new Mock&lt;T&gt;() and any of it’s dependencies will be satisfied by mocks.</p>
<h4>How do I use the UnityAutoMockContainer?</h4>
<p>It’s pretty basic, you first create an instance of the UnityAutoMockContainer, and from there you can ask it for mocks of an (Interface, Class, or Abstract Class).</p>
<p>If you request an instance of a concrete class, or abstract class, the UnityAutoMockContainer will stuff mocks in for any constructor dependencies of your concrete class (if it can). You can then request from the container those same dependencies one at a time and either apply mocking setups or verifications.</p>
<blockquote>
<p><strong>NOTE:</strong> Anything the container creates will live as a singleton instance in the container. So any other requests from the container will always return the originally created instance. Therefore, each distinct scenario in a test suite should have their own instances of the container.</p>
</blockquote>
<p>Below his an example of how you can leverage the container in some tests. Given this base fixture class…</p>
<pre class="brush: csharp;">public class FixtureBase
{
    private readonly UnityAutoMockContainer _autoMockContainer = new UnityAutoMockContainer();

    protected UnityAutoMockContainer AutoMockContainer
    {
        get { return _autoMockContainer; }
    }

    [TestFixtureSetUp]
    public void SetupContext_ALL()
    {
        Before_all_tests();
        Because();
    }

    [TestFixtureTearDown]
    public void TearDownContext_ALL()
    {
        After_all_tests();
    }

    protected virtual void Before_all_tests()
    {
    }

    protected virtual void Because()
    {
    }

    protected virtual void After_all_tests()
    {
    }
}</pre>
<p>If I were given the following system to test.</p>
<pre class="brush: csharp;">public interface IServiceA { void RunA(); }
public interface IServiceB { void RunB(); }

public class TestComponent
{
    public TestComponent(IServiceA serviceA, IServiceB serviceB)
    {
        ServiceA = serviceA;
        ServiceB = serviceB;
    }

    public IServiceA ServiceA { get; private set; }
    public IServiceB ServiceB { get; private set; }

    public void RunAll()
    {
        if (!HowDidItGo())
            return;
        ServiceA.RunA();
        ServiceB.RunB();
    }

    public virtual bool HowDidItGo()
    {
        // some really nasty untestable code
        return true;
    }
}</pre>
<p>The below example demonstrates simply verifying some behavior on the mocked dependencies of the system under test.</p>
<pre class="brush: csharp;">[TestFixture]
public class Example__how_to_pull_items_from_the_UnityAutoMockContainer_when_verifying_behavior_after_an_action_was_taken
    : FixtureBase
{
    private TestComponent _testComponent;

    protected override void Before_all_tests()
    {
        base.Before_all_tests();
        _testComponent = AutoMockContainer.Resolve&lt;TestComponent&gt;();
    }

    protected override void Because()
    {
        _testComponent.RunAll();
    }

    [Test]
    public void Should_run_ServiceA_RunA()
    {
        AutoMockContainer
            .GetMock&lt;IServiceA&gt;()
            .Verify(v =&gt; v.RunA(), Times.Once());
    }

    [Test]
    public void Should_run_ServiceB_RunB()
    {
        AutoMockContainer
            .GetMock&lt;IServiceB&gt;()
            .Verify(v =&gt; v.RunB(), Times.Once());
    }
}</pre>
<p>Next, you may have noticed that the system under test had a complicated internal method (that may not necessarily be testable). You can use the AutoMocker to create the system under test as a Mock itself, so we can override some of the behavior. Here’s how you could quickly do that. </p>
<blockquote>
<p><strong>Aside</strong>: I’m not saying this is a good practice or aids in good component design, just saying it’s possible</p>
</blockquote>
<pre class="brush: csharp;">[TestFixture]
public class Example__how_to_use_the_UnityAutoMockContainer_to_override_a_method_on_the_SystemUnderTest_to_test_a_certain_behavior
    : FixtureBase
{

    private TestComponent _testComponent;

    protected override void Before_all_tests()
    {
        base.Before_all_tests();
        var mockTestComponent = AutoMockContainer.GetMock&lt;TestComponent&gt;();

        mockTestComponent
            .Setup(s =&gt; s.HowDidItGo())
            .Returns(false);

        _testComponent = mockTestComponent.Object;
    }

    protected override void Because()
    {
        _testComponent.RunAll();
    }

    [Test]
    public void Should_run_ServiceA_RunA()
    {
        AutoMockContainer
            .GetMock&lt;IServiceA&gt;()
            .Verify(v =&gt; v.RunA(), Times.Never());
    }

    [Test]
    public void Should_run_ServiceB_RunB()
    {
        AutoMockContainer
            .GetMock&lt;IServiceB&gt;()
            .Verify(v =&gt; v.RunB(), Times.Never());
    }
}</pre>
<p>&#160;</p>
<p>It’s amazing how much redundant test setup code this little helper has saved me in my tests. I hope others can find some use with this as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/03/unitymoq-automocker-or-automockingcontainer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Specifications</title>
		<link>http://elegantcode.com/2009/12/22/specifications/</link>
		<comments>http://elegantcode.com/2009/12/22/specifications/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 03:30:50 +0000</pubDate>
		<dc:creator>Mark Nijhof</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[Conventions]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/22/specifications/</guid>
		<description><![CDATA[I received a couple questions about the Specification Framework that I use in the CQRS example and thought lets talk about that for a bit. The first thing that should be underlined is that this is _not_ a framework, they are a few classes and extension methods that rely on NUnit for the actual assertions [...]]]></description>
			<content:encoded><![CDATA[<p>I received a couple questions about the Specification Framework that I use in the <a href="http://github.com/MarkNijhof/Fohjin/tree/f85a25181b4fa382bd3afbbbbcb08da891cc8e45/Fohjin.DDD.Example" target="_blank">CQRS example</a> and thought lets talk about that for a bit. The first thing that should be underlined is that this is _not_ a framework, they are a few <a href="http://github.com/MarkNijhof/Fohjin/tree/f85a25181b4fa382bd3afbbbbcb08da891cc8e45/Fohjin.DDD.Example/Test.Fohjin.DDD" target="_blank">classes and extension methods</a> that rely on <a href="http://www.nunit.org/index.php" target="_blank">NUnit</a> for the actual assertions and and <a href="http://code.google.com/p/moq/" target="_blank">Moq</a> for mocking of the dependencies. I got the initial bits from Greg Young at his DDD course which I extended a little bit for my specific needs. </p>
<p>I have the following base test fixture classes:</p>
<ul>
<li>BaseTestFixture </li>
<li>BaseTestFixture&lt;TSubjectUnderTest&gt; </li>
<li>AggregateRootTestFixture&lt;TAggregateRoot&gt; </li>
<li>CommandTestFixture&lt;TCommand, TCommandHandler, TAggregateRoot&gt; </li>
<li>EventTestFixture&lt;TEvent, TEventHandler&gt; </li>
<li>PresenterTestFixture&lt;TPresenter&gt; </li>
</ul>
<p>These different classes are all very specific towards a specific need, which is a direct opposite from what a framework usually provides. </p>
<h2>Black box</h2>
<p>I try my best to make my tests treat the subject under test (SUT) as a black box, meaning that in my tests I don’t directly interact with the actual class that I am testing. Instead I want to trigger the behavior by executing behavior that lies further outside. The behavior that triggers the behavior on the SUT may be an actual implementation, or it could be a fake. </p>
<p>The same applies to the result of the behavior that gets tested, instead of verifying some state in the SUT I want to verify what happens outside of the SUT. So what matters is that I test the behavior not the state of the subject under test.</p>
<p>I also try to get further away from the SUT then its immediate usage. Doing so makes the tests less brittle for change. This in itself is not always an easy task, but I recommend you try it anyway. </p>
<h2>The BaseTestFixture</h2>
<p>This is the simplest test fixture class that I have and I use this for and I actually don’t use this anywhere in the example code, but it serves a really good basic overview of the semantics that are shared among the other test fixture classes. </p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Specification</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Exception</span> CaughtException;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Given() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">void</span> When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Finally() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Given</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Setup()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span>();&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Given();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">try</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">catch</span> (<span style="color: #2b91af">Exception</span> exception)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> exception;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">finally</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Finally();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">GivenAttribute</span> : <span style="color: #2b91af">SetUpAttribute</span> { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">ThenAttribute</span> : <span style="color: #2b91af">TestAttribute</span> { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">SpecificationAttribute</span> : <span style="color: #2b91af">TestFixtureAttribute</span> { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span> }</pre>
</div>
<p></p>
<p>It is following the Given When Then (GWT) approach, and as you can see it is really simple. Also note that I introduced some other named attributes by simply inheriting from the default NUnit attributes, this was purely done to stay with the GWT approach.</p>
<p>Below here is an incredible KISS example of how you would use this BaseTestFixture, which I believe doesn’t need further explanation. (I know I am misusing the term KISS here, but I thought if was fitting anyway).</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Very_adding_a_number_to_the_subject_under_test</span> : <span style="color: #2b91af">BaseTestFixture</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">int</span> SubjectUnderTest;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> Given()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest <span style="color: aqua">=</span> <span style="color: yellow">2</span>;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest <span style="color: aqua">+=</span> <span style="color: yellow">2</span>;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_new_value_of_the_subject_under_test_will_be_4()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest<span style="color: aqua">.</span>WillBe(<span style="color: yellow">4</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span> }</pre>
</div>
<h2>The BaseTestFixture&lt;TSubjectUnderTest&gt;</h2>
<p>Now we are getting into a more interesting case because now my subject under test is actually provided by the generic parameter of the base test fixture class. And to be honest this class is only used with 12 specifications out of the 122 specification classes. This is mostly because it is still a very generic solution, but again a nice way to ease into it.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Specification</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">BaseTestFixture</span><span style="color: aqua">&lt;</span>TSubjectUnderTest<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> DoNotMock;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> TSubjectUnderTest SubjectUnderTest;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Exception</span> CaughtException;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> SetupDependencies() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Given() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">void</span> When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Finally() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Given</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Setup()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; DoNotMock <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; BuildMocks();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SetupDependencies();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest <span style="color: aqua">=</span> BuildSubjectUnderTest();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Given();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">try</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">catch</span> (<span style="color: #2b91af">Exception</span> exception)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> exception;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">finally</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Finally();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> OnDependency<span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>() <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> TSubjectUnderTest BuildSubjectUnderTest()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(TSubjectUnderTest)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> parameters <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">List</span><span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">foreach</span> (<span style="color: #0080c0">var</span> mock <span style="color: #0080c0">in</span> mocks)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">object</span> theObject;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>DoNotMock<span style="color: aqua">.</span>TryGetValue(mock<span style="color: aqua">.</span>Key, <span style="color: #0080c0">out</span> theObject))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; theObject <span style="color: aqua">=</span> ((<span style="color: #2b91af">Mock</span>) mock<span style="color: aqua">.</span>Value)<span style="color: aqua">.</span>Object;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; parameters<span style="color: aqua">.</span>Add(theObject);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (TSubjectUnderTest)constructorInfo<span style="color: aqua">.</span>Invoke(parameters<span style="color: aqua">.</span>ToArray());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">void</span> BuildMocks()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(TSubjectUnderTest)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">foreach</span> (<span style="color: #0080c0">var</span> parameter <span style="color: #0080c0">in</span> constructorInfo<span style="color: aqua">.</span>GetParameters())</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks<span style="color: aqua">.</span>Add(parameter<span style="color: aqua">.</span>ParameterType, CreateMock(parameter<span style="color: aqua">.</span>ParameterType));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">static</span> <span style="color: #0080c0">object</span> CreateMock(<span style="color: #2b91af">Type</span> type)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;&gt;</span>)<span style="color: aqua">.</span>MakeGenericType(type)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> constructorInfo<span style="color: aqua">.</span>Invoke(<span style="color: #0080c0">new</span> <span style="color: #0080c0">object</span>[] { });</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 80</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 81</span> }</pre>
</div>
<p></p>
<p>Wow there is _a lot_ more going on here! You are right, because here I make the base test fixture responsible for instantiating the subject under test, including providing mocks for any dependencies that it may have. So it is an auto mocker as well, but the interesting part here is that it puts a reference of the injected mocks in a collection that you can access inside your tests by using the OnDependency&lt;TType&gt; method that returns a Moq object.</p>
<p>Take a look at a specification using this base test fixture class:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Scenarios<span style="color: aqua">.</span>Receiving_money_transfer</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">When_receiving_a_money_transfer</span> : <span style="color: #2b91af">BaseTestFixture</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">MoneyReceiveService</span><span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> SetupDependencies()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span><span style="color: #2b91af">IReportingRepository</span><span style="color: aqua">&gt;</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>GetByExample<span style="color: aqua">&lt;</span><span style="color: #2b91af">AccountReport</span><span style="color: aqua">&gt;</span>(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>()))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Returns(<span style="color: #0080c0">new</span> <span style="color: #2b91af">List</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">AccountReport</span><span style="color: aqua">&gt;</span> { <span style="color: #0080c0">new</span> <span style="color: #2b91af">AccountReport</span>(<span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #a31515">&quot;AccountName&quot;</span>, <span style="color: #a31515">&quot;target account number&quot;</span>) });</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SubjectUnderTest<span style="color: aqua">.</span>Receive(<span style="color: #0080c0">new</span> <span style="color: #2b91af">MoneyTransfer</span>(<span style="color: #a31515">&quot;source account number&quot;</span>, <span style="color: #a31515">&quot;target account number&quot;</span>, <span style="color: yellow">123.45M</span>));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_newly_created_account_will_be_saved()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span><span style="color: #2b91af">IBus</span><span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Verify(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Publish(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #2b91af">ReceiveMoneyTransferCommand</span><span style="color: aqua">&gt;</span>()));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span> }</pre>
</div>
<p></p>
<p>So the first thing you see is the method SetupDependencies that is requesting the mock object for injected type IReporintgRepository and it is using the Moq way of setting up the Mock object. This is only needed when in your specification you need the mocks to be setup in a specific way. I intentionally separated the SetupDependencies from the Given as they may be two different things. And in the actual test you see the usage of the OnDependency method again where its being used to verify that something was indeed triggered on the injected class.</p>
<p>Now indeed this is not really treating the subject under test as a black box, for example in the When I make a direct call to a method on the subject under test. So my test knows about this now, meaning when it changes I need to change this test as well. So lets take a look where I go a bit further into the black box mentality.</p>
<h2>The PresenterTestFixture&lt;TPresenter&gt;</h2>
<p>Here I am not going to show you the code of the PresenterTestFixture implementation as it is almost identical the the previous base test fixture. So lets go straight to an actual specification:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Scenarios<span style="color: aqua">.</span>Adding_a_new_client</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">When_in_the_GUI_the_phone_number_of_the_new_client_is_saved</span> : <span style="color: #2b91af">PresenterTestFixture</span><span style="color: aqua">&lt;</span>ClientDetailsPresenter<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">object</span> CreateClientCommand;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> SetupDependencies()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span>IPopupPresenter<span style="color: aqua">&gt;</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>CatchPossibleException(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&gt;</span>()))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Callback<span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&gt;</span>(x <span style="color: aqua">=&gt;</span> x());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OnDependency<span style="color: aqua">&lt;</span>IBus<span style="color: aqua">&gt;</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Publish(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>()))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: aqua">.</span>Callback<span style="color: aqua">&lt;</span><span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>(x <span style="color: aqua">=&gt;</span> CreateClientCommand <span style="color: aqua">=</span> x);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> Given()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Presenter<span style="color: aqua">.</span>SetClient(<span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Presenter<span style="color: aqua">.</span>Display();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>ClientName)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;New Client Name&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnFormElementGotChanged <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnSaveNewClientName <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Street)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;Street&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>StreetNumber)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;123&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>PostalCode)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;5000&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>City)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;Bergen&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnFormElementGotChanged <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnSaveNewAddress <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ValueFor(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>PhoneNumber)<span style="color: aqua">.</span>IsSetTo(<span style="color: #a31515">&quot;1234567890&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnFormElementGotChanged <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #0080c0">void</span> When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>FireEvent(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>OnSaveNewPhoneNumber <span style="color: aqua">+=</span> <span style="color: #0080c0">null</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_save_button_will_be_disabled()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>VerifyThat<span style="color: aqua">.</span>Method(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>DisableSaveButton())<span style="color: aqua">.</span>WasCalled();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_a_create_client_command_with_all_collected_information_will_be_published()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IBus<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>VerifyThat<span style="color: aqua">.</span>Method(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Publish(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()))<span style="color: aqua">.</span>WasCalled();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>ClientName<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;New Client Name&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Street<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;Street&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>StreetNumber<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;123&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>PostalCode<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;5000&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>City<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;Bergen&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CreateClientCommand<span style="color: aqua">.</span>As<span style="color: aqua">&lt;</span>CreateClientCommand<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>PhoneNumber<span style="color: aqua">.</span>WillBe(<span style="color: #a31515">&quot;1234567890&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_overview_panel_will_be_shown()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; On<span style="color: aqua">&lt;</span>IClientDetailsView<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>VerifyThat<span style="color: aqua">.</span>Method(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Close())<span style="color: aqua">.</span>WasCalled();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span> }</pre>
</div>
<p></p>
<p>Again there is the setting up of a dependency in the beginning and then there is the Given method, Presenter in this case is the subject under test, so you can see that the specification still knows about the SUT in the Given, but you will notice that this is not used in either the When or the Then.</p>
<p>Hey what is that On thing in there? Well that is a very small DSL wrapping the Moq API. I did this to make it slightly more readable, and in this case it is very adapt towards working with a view and presenter. In the Given I am setting up my IClientDetailsView with the correct data, but I am also simulating that an event was triggered. This is not logic that the test is concerned about, all we do here is bring the view and the presenter in the correct state for this particular specification. So instead of setting these things directly on the presenter this will all be directed from the view.</p>
<p>Then in the When we again fire an event from the view, but in this case it is the going to trigger the behavior on the presenter that we want to verify. And in the Then methods we verify on the view again that the presenter actually did the correct things, but we also verify on other dependencies that the correct methods where called, in this case the IBus.</p>
<p>I am not completely happy with the mini DSL yet, but I think it is cleaner then the default Moq API. Just for those that are curious, here is the mini DSL which gets returned by the On method:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">MockDsl</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> MockDsl(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">ValueSetter</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;</span> ValueFor<span style="color: aqua">&lt;</span>TProperty<span style="color: aqua">&gt;</span>(<span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;&gt;</span> selector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ValueSetter</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;</span>(_mocks, selector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> FireEvent(<span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> fieldSelector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>Raise(fieldSelector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">Verifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> VerifyThat { <span style="color: #0080c0">get</span> { <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Verifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>(_mocks); } }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">Verifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> Verifier(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> ValueIsSetFor(<span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> selector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>VerifySet(selector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">MethodVerifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> Method(<span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;&gt;</span> selector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">MethodVerifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>(_mocks, selector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">MethodVerifier</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;&gt;</span> _fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> MethodVerifier(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks, <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Action</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;&gt;</span> fieldSelector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _fieldSelector <span style="color: aqua">=</span> fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> WasCalled()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>Verify(_fieldSelector);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">ValueSetter</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;</span> <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 75</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> _mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">readonly</span> <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;&gt;</span> _fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 78</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> ValueSetter(<span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks, <span style="color: #2b91af">Expression</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Func</span><span style="color: aqua">&lt;</span>TType, TProperty<span style="color: aqua">&gt;&gt;</span> fieldSelector)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 80</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 81</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _mocks <span style="color: aqua">=</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 82</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _fieldSelector <span style="color: aqua">=</span> fieldSelector;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 83</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 84</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 85</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> IsSetTo(TProperty value)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 86</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 87</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (<span style="color: aqua">!</span>_mocks<span style="color: aqua">.</span>ContainsKey(<span style="color: #0080c0">typeof</span>(TType)))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 88</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">throw</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Exception</span>(<span style="color: #0080c0">string</span><span style="color: aqua">.</span>Format(<span style="color: #a31515">&quot;The requested dependency '{0}' is not specified in the constructor&quot;</span>, <span style="color: #0080c0">typeof</span>(TType)<span style="color: aqua">.</span>FullName));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 89</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 90</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> mock <span style="color: aqua">=</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)_mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 91</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mock<span style="color: aqua">.</span>SetupGet(_fieldSelector)<span style="color: aqua">.</span>Returns(value);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 92</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 93</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 94</span> }</pre>
</div>
<p></p>
<h2>CQRS and Event Sourcing</h2>
<p>By combining <a href="http://elegantcode.com/2009/11/11/cqrs-la-greg-young/" target="_blank">CQRS and Event Sourcing</a> we get an architecture that is very suitable for black box testing its behavior, which was a real eye opener when Greg demonstrated this to me. He says that the way to bring your aggregate root back into the desired state is to playback the events that are needed to do so. Then you can execute the behavior on the aggregate root, and finally to actually verify your behavior you retrieve the published events and verify that they are as expected. </p>
<p>Now the beauty of this is that the setup and the verification this will work on any aggregate root because we are using the IEventProvider interface there that they all implement. The only actual knowledge about the aggregate root that remains is the specific behavior that you trigger.</p>
<p>But I went a little bit further then what was shown during the course, I am saying that instead of executing the behavior on the aggregate root we could also just provide the command that should trigger this behavior to be executed. Because the command would be handled by a specific command handler which in turn would execute the domain behavior.</p>
<p>Now below here is the command test fixture that allows me to do just that, I need to provide the actual command, command handler and aggregate root types that are to be used in this specification.</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Specification</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">abstract</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">CommandTestFixture</span><span style="color: aqua">&lt;</span>TCommand, TCommandHandler, TAggregateRoot<span style="color: aqua">&gt;</span> </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TCommand : <span style="color: #0080c0">class</span>, ICommand</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TCommandHandler : <span style="color: #0080c0">class</span>, ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">where</span> TAggregateRoot : <span style="color: #0080c0">class</span>, IOrginator, IEventProvider<span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span>, <span style="color: #0080c0">new</span>()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #2b91af">IDictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span> mocks;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> TAggregateRoot AggregateRoot;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span> CommandHandler;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">Exception</span> CaughtException;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> PublishedEvents;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> SetupDependencies() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> Given() </pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">List</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">virtual</span> <span style="color: #0080c0">void</span> Finally() { }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">abstract</span> TCommand When();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Given</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Setup()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Dictionary</span><span style="color: aqua">&lt;</span><span style="color: #2b91af">Type</span>, <span style="color: #0080c0">object</span><span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AggregateRoot <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> TAggregateRoot();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AggregateRoot<span style="color: aqua">.</span>LoadFromHistory(Given());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 30</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CommandHandler <span style="color: aqua">=</span> BuildCommandHandler();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 32</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; SetupDependencies();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 34</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">try</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 35</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 36</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CommandHandler<span style="color: aqua">.</span>Execute(When());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents <span style="color: aqua">=</span> AggregateRoot<span style="color: aqua">.</span>GetChanges();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">catch</span> (<span style="color: #2b91af">Exception</span> exception)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CaughtException <span style="color: aqua">=</span> exception;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 43</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">finally</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 44</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 45</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Finally();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 48</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span> OnDependency<span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>() <span style="color: #0080c0">where</span> TType : <span style="color: #0080c0">class</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>TType<span style="color: aqua">&gt;</span>)mocks[<span style="color: #0080c0">typeof</span>(TType)];</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 52</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 53</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 54</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span> BuildCommandHandler()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 55</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 56</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span>(TCommandHandler)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 57</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 58</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">foreach</span> (<span style="color: #0080c0">var</span> parameter <span style="color: #0080c0">in</span> constructorInfo<span style="color: aqua">.</span>GetParameters())</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 59</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 60</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">if</span> (parameter<span style="color: aqua">.</span>ParameterType <span style="color: aqua">==</span> <span style="color: #0080c0">typeof</span>(IDomainRepository<span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span>))</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 61</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 62</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> repositoryMock <span style="color: aqua">=</span> <span style="color: #0080c0">new</span> <span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;</span>IDomainRepository<span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 63</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; repositoryMock<span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>GetById<span style="color: aqua">&lt;</span>TAggregateRoot<span style="color: aqua">&gt;</span>(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span><span style="color: #2b91af">Guid</span><span style="color: aqua">&gt;</span>()))<span style="color: aqua">.</span>Returns(AggregateRoot);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 64</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; repositoryMock<span style="color: aqua">.</span>Setup(x <span style="color: aqua">=&gt;</span> x<span style="color: aqua">.</span>Add(<span style="color: #2b91af">It</span><span style="color: aqua">.</span>IsAny<span style="color: aqua">&lt;</span>TAggregateRoot<span style="color: aqua">&gt;</span>()))<span style="color: aqua">.</span>Callback<span style="color: aqua">&lt;</span>TAggregateRoot<span style="color: aqua">&gt;</span>(x <span style="color: aqua">=&gt;</span> AggregateRoot <span style="color: aqua">=</span> x);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 65</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks<span style="color: aqua">.</span>Add(parameter<span style="color: aqua">.</span>ParameterType, repositoryMock);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 66</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">continue</span>;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 67</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 68</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 69</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; mocks<span style="color: aqua">.</span>Add(parameter<span style="color: aqua">.</span>ParameterType, CreateMock(parameter<span style="color: aqua">.</span>ParameterType));</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 70</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 71</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 72</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> (ICommandHandler<span style="color: aqua">&lt;</span>TCommand<span style="color: aqua">&gt;</span>)constructorInfo<span style="color: aqua">.</span>Invoke(mocks<span style="color: aqua">.</span>Values<span style="color: aqua">.</span>Select(x <span style="color: aqua">=&gt;</span> ((<span style="color: #2b91af">Mock</span>) x)<span style="color: aqua">.</span>Object)<span style="color: aqua">.</span>ToArray());</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 73</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 74</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 75</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">private</span> <span style="color: #0080c0">static</span> <span style="color: #0080c0">object</span> CreateMock(<span style="color: #2b91af">Type</span> type)</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 76</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 77</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">var</span> constructorInfo <span style="color: aqua">=</span> <span style="color: #0080c0">typeof</span> (<span style="color: #2b91af">Mock</span><span style="color: aqua">&lt;&gt;</span>)<span style="color: aqua">.</span>MakeGenericType(type)<span style="color: aqua">.</span>GetConstructors()<span style="color: aqua">.</span>First();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 78</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> constructorInfo<span style="color: aqua">.</span>Invoke(<span style="color: #0080c0">new</span> <span style="color: #0080c0">object</span>[]{});</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 79</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 80</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 81</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 82</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">ThereWasNoExceptionButOneWasExpectedException</span> : <span style="color: #2b91af">Exception</span> {}</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 83</span> }</pre>
</div>
<p></p>
<p>Please note that the Given method now returns an IEnumerable&lt;IDomainEvent&gt; this is to be used to provide the events that are needed to bring the aggregate root into the correct state for this specification. This is using the exact same mechanism as the actual code uses to make state changes in the aggregate root, so there cannot be a case that you are testing your aggregate root using a state that it cannot get into.</p>
<p>The When method returns the expected command, so all you do there is create the command with the correct information and return it to the specification.</p>
<p>Then in the try catch block you may have noticed that a command handler is executing the provided command and that after that the events are being retrieved from the aggregate root. These events are what you would verify to make sure your domain behavior is correct. </p>
<p>But this may all sound very abstract, lets look at a simple specification and see how clean and readable is really is:</p>
<div style="padding-bottom: 10px; margin: 0px; font-family: monaco,verdana; background: rgb(22,36,51); color: white; font-size: 10pt; overflow: scroll; padding-top: 10px; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 1</span>&#160;<span style="color: #0080c0">namespace</span> Test<span style="color: aqua">.</span>Fohjin<span style="color: aqua">.</span>DDD<span style="color: aqua">.</span>Scenarios<span style="color: aqua">.</span>Withdrawing_cash</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 2</span> {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">class</span> <span style="color: #2b91af">When_withdrawing_cash</span> : <span style="color: #2b91af">CommandTestFixture</span><span style="color: aqua">&lt;</span>WithdrawlCashCommand, WithdrawlCashCommandHandler, ActiveAccount<span style="color: aqua">&gt;</span></pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> <span style="color: #2b91af">IEnumerable</span><span style="color: aqua">&lt;</span>IDomainEvent<span style="color: aqua">&gt;</span> Given()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">yield</span> <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> AccountOpenedEvent(<span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: #a31515">&quot;AccountName&quot;</span>, <span style="color: #a31515">&quot;1234567890&quot;</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">yield</span> <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> CashDepositedEvent(<span style="color: yellow">20</span>, <span style="color: yellow">20</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 10</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">protected</span> <span style="color: #0080c0">override</span> WithdrawlCashCommand When()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">return</span> <span style="color: #0080c0">new</span> WithdrawlCashCommand(<span style="color: #2b91af">Guid</span><span style="color: aqua">.</span>NewGuid(), <span style="color: yellow">5</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 15</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 16</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_a_cash_withdrawn_event_will_be_published()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents<span style="color: aqua">.</span>Last()<span style="color: aqua">.</span>WillBeOfType<span style="color: aqua">&lt;</span>CashWithdrawnEvent<span style="color: aqua">&gt;</span>();</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 21</span>&#160;</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [<span style="color: #2b91af">Then</span>]</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #0080c0">public</span> <span style="color: #0080c0">void</span> Then_the_published_event_will_contain_the_amount_and_new_account_balance()</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 25</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents<span style="color: aqua">.</span>Last<span style="color: aqua">&lt;</span>CashWithdrawnEvent<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Balance<span style="color: aqua">.</span>WillBe(<span style="color: yellow">15</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 26</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; PublishedEvents<span style="color: aqua">.</span>Last<span style="color: aqua">&lt;</span>CashWithdrawnEvent<span style="color: aqua">&gt;</span>()<span style="color: aqua">.</span>Amount<span style="color: aqua">.</span>WillBe(<span style="color: yellow">5</span>);</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 28</span>&#160;&#160;&#160;&#160; }</pre>
<pre style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; background: rgb(22,36,51); padding-top: 0px"><span style="background: #121e31">&#160;&#160; 29</span> }</pre>
</div>
<p></p>
<p>So you provide historical events to bring the aggregate root into the expected state, you fire of the command, then you verify the published events to ensure your domain behavior is correct. If you choose the correct naming for your events and commands, then a business person would be able to understand the specification. Especially if you parse the text and do a little bit of formatting.</p>
<h2>Where is Should?</h2>
<p> Hey what are all those WillBe and WillBeOfType things that I see in your specifications, should they not be ShouldBe and ShouldBeOfType? Well I used to think so as well, until I attended a presentation by <a href="http://arkiv.ndc2009.no/agenda.aspx?cat=1071&amp;id=1813&amp;day=3728" target="_blank">Kevlin Henney at NDC</a> where he explained that Should is not specific enough. Should indicates that it might not. I like to use the example; “I should really do the dishes, but I won’t”. By using Will Be and Must you are much more dictating what will or must happen, its not a question anymore. </p>
<h2>Finally</h2>
<p>I am completely taken by this approach and as you see you don’t need a big BDD framework for this. I think using something like this gives a good learning experience before going towards an actual BDD framework like <a href="http://codebetter.com/blogs/aaron.jensen/archive/2008/05/08/introducing-machine-specifications-or-mspec-for-short.aspx" target="_blank">MSpec</a>. Also Uncle Bob just wrote a good post about to <a href="http://blog.objectmentor.com/articles/2009/12/19/the-polyglot-tester" target="_blank">not abuse the Given When Then approach</a> and also take a look at the <a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank">Mocks aren’t Stubs</a> article by Martin Fowler.</p>
<p>By now you must &lt;grin&gt; understand that I like to throw with code examples so yes the post is very long, but I hope it provides enough value instead of just confusion. </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/22/specifications/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>StatLight &#8211; Goes Open Source</title>
		<link>http://elegantcode.com/2009/12/10/statlight-goes-open-source/</link>
		<comments>http://elegantcode.com/2009/12/10/statlight-goes-open-source/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 03:26:55 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/10/statlight-goes-open-source/</guid>
		<description><![CDATA[Although I made a very minor attempt at making StatLight a “for-sale” product, I knew when I started that open-source was most likely going to be my long term path for StatLight.
What is it? (Silverlight Testing Automation Tool)
StatLight is a tool developed for automating the setup, running, and gathering results of Silverlight unit tests. StatLight [...]]]></description>
			<content:encoded><![CDATA[<p>Although I made a very minor attempt at making <a href="http://statlight.net" target="_blank">StatLight</a> a “for-sale” product, I knew when I started that open-source was most likely going to be my long term path for <a href="http://statlight.net" target="_blank">StatLight</a>.</p>
<h5>What is it? (Silverlight Testing Automation Tool)</h5>
<p>StatLight is a tool developed for automating the setup, running, and gathering results of Silverlight unit tests. StatLight helps to speed up the feedback cycles while practicing<a href="http://en.wikipedia.org/wiki/Test-driven_development"> TDD</a>/<a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a>/(insert your test style here) during Silverlight development.</p>
<h5>Where can I get StatLight?</h5>
<p><a href="http://StatLight.CodePlex.com">http://StatLight.CodePlex.com</a></p>
<h4>Happy Coding !!!</h4>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/10/statlight-goes-open-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrapping up the StructureMap Automocking Container</title>
		<link>http://elegantcode.com/2009/11/30/wrapping-up-the-structuremap-automocking-container/</link>
		<comments>http://elegantcode.com/2009/11/30/wrapping-up-the-structuremap-automocking-container/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 07:21:43 +0000</pubDate>
		<dc:creator>Richard Cirerol</dc:creator>
				<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[StructureMap; Automocking; MSpec;]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/11/30/wrapping-up-the-structuremap-automocking-container/</guid>
		<description><![CDATA[<p><font color="#804040"><strong>UPDATE</strong>: I apologize…the original wrapper did not allow injected dependencies.&#160; I have fixed this bug and updated the sample.</font></p> I have been using StructureMap.AutoMocking with MSpec (Machine.Specifications) and Rhino.Mocks for a few months now.  Although I am very comfortable with the patterns that emerge from using the frameworks together, introducing StructureMap.Automocking to other developers is sometimes challenging. I decided to wrap the details of the container to make the intention clearer and explanation easier.  I have started using this wrapper in my projects, and have found it makes building up specifications more efficient in my daily coding.]]></description>
			<content:encoded><![CDATA[<p><span style="color: #804040;"><strong>UPDATE</strong>: I apologize…the original wrapper did not allow injected dependencies.  I have fixed this bug and updated the sample.</span></p>
<p>I have been using StructureMap.AutoMocking with MSpec (Machine.Specifications) and Rhino.Mocks for a few months now.  Although I am very comfortable with the patterns that emerge from using the frameworks together, introducing StructureMap.Automocking to other developers is sometimes challenging.</p>
<p>When viewing a specification or test set up with the Rhino.Mocks or Moq automocking container, what is being tested is not readily apparent.  Once the concept is explained and demonstrated, these same developers seem to have no issue with maintaining the specifications/tests.</p>
<p>I decided to wrap the details of the container to make the intention clearer and explanation easier.  I have started using this wrapper in my projects, and have found it makes building up specifications more efficient in my daily coding.</p>
<p>Let me know whether this is useful to you, or how it could be made more useful.</p>
<h4>The Examples</h4>
<p>Let’s contrive an example.  We’ll say we have a coffee machine that grinds its own beans before brewing. However, the hopper has to have beans before starting the grind.  Here is the basic specification:</p>
<table border="1" cellspacing="0" cellpadding="2" width="533">
<tbody>
<tr>
<td width="117" valign="top">Action/Behavior</td>
<td width="414" valign="top">Prepare coffee grounds for 12 cups of coffee</td>
</tr>
<tr>
<td rowspan="2" width="117" valign="top">Expectations</td>
<td width="414" valign="top">Should check that hopper has beans (mock returns true)</td>
</tr>
<tr>
<td width="414" valign="top">Because hopper has beans, should ask grinder to grind enough beans for 12 cups of coffee</td>
</tr>
</tbody>
</table>
<p>We could start without a container, using Rhino.Mocks to build up the CoffeeMachine dependencies manually:</p>
<pre class="brush: csharp;">[Subject(typeof(CoffeeMachine),"using vanilla Rhino.Mocks")]
public class Example01_when_preparing_coffee_grounds
{
    Establish context = () =&gt;
        {
            Grinder = MockRepository.GenerateMock&lt;IGrinder&gt;();
            Hopper = MockRepository.GenerateMock&lt;IHopper&gt;();
            Hopper.Expect(x =&gt; x.HasBeans()).Return(true);
            CoffeeMachine = new CoffeeMachine(Grinder, Hopper);
        };

    Because of = () =&gt; _coffeeMachine.PrepareCoffeeGrounds(12);

    It should_check_if_hopper_has_beans = () =&gt; Hopper.VerifyAllExpectations();
    It should_grind_coffee = () =&gt; Grinder.AssertWasCalled(x =&gt; x.Grind(12));

    static IGrinder Grinder;
    static IHopper Hopper;
    static CoffeeMachine CoffeeMachine;
}</pre>
<p>For those of you not familiar with the MSpec style, please refer to the following posts:</p>
<ul>
<li><a href="http://blog.wekeroad.com/blog/make-bdd-your-bff-2/" target="_blank">Make BDD Your BFF</a></li>
<li><a href="http://www.lostechies.com/blogs/seanbiefeld/archive/2009/08/25/step-by-step-to-using-machine-specifications-with-resharper.aspx" target="_blank">Step by Step to Using MSpec (Machine.Specifications) with ReSharper</a></li>
<li><a href="http://elegantcode.com/2009/07/05/mspec-take-2/" target="_blank">MSpec &#8211; Take 2</a></li>
<li><a href="http://marcinobel.com/index.php/mspec-bdd-installer/" target="_blank">MSpec BDD framework installer</a></li>
<li><strong>Updated! </strong>From the <a href="http://blog.cwa.me.uk/2009/11/30/the-morning-brew-487/" target="_blank">Morning Brew #487</a>: BDD with MSpec and Rhino Auto Mocks, parts  <a href="http://telldontask.wordpress.com/2009/11/10/bdd-with-mspec-and-rhino-auto-mocks/" target="_blank">1</a>, <a href="http://telldontask.wordpress.com/2009/11/19/bdd-with-mspec-and-rhino-auto-mocks-part-2/" target="_blank">2</a>, and <a href="http://telldontask.wordpress.com/2009/11/29/bdd-with-mspec-and-rhino-auto-mocks-part-3/" target="_blank">3</a><br />
Here is the same specification using the RhinoAutoMocker&lt;T&gt; class provided by StructureMap.AutoMocking:</li>
</ul>
<pre class="brush: csharp;">[Subject(typeof(CoffeeMachine),"using RhinoAutoMocker&lt;CoffeeMachine&gt;")]
public class Example02_when_preparing_coffee_grounds
{
    Establish context = () =&gt;
        {
            CoffeeMachine = new RhinoAutoMocker&lt;CoffeeMachine&gt;();
            Grinder = CoffeeMachine.Get&lt;IGrinder&gt;();
            Hopper = CoffeeMachine.Get&lt;IHopper&gt;();
            Hopper.Expect(x =&gt; x.HasBeans()).Return(true);
        };

    Because of = () =&gt; v.ClassUnderTest.PrepareCoffeeGrounds(12);

    It should_check_if_hopper_has_beans = () =&gt; Hopper.VerifyAllExpectations();
    It should_grind_coffee = () =&gt; Grinder.AssertWasCalled(x =&gt; x.Grind(12));

    static IGrinder Grinder;
    static IHopper Hopper;
    static RhinoAutoMocker&lt;CoffeeMachine&gt; CoffeeMachine;
}</pre>
<p>Although I think this looks cleaner, the initialization of CoffeeMachine causes some confusion. Unfortunately, a common question would be, “Is RhinoAutoMocker&lt;CoffeeMachine&gt; creating a mocked version of CoffeeMachine?” Well, not exactly.</p>
<p>RhinoAutoMocker is an implementation of the AutoMocker base class which uses StructureMap to fill dependencies.  The RhinoAutoMocker implementation of the container uses Rhino.Mocks to generate all of dependencies of the target class, whereas the MoqAutoMocker implementation uses Moq.  (Optionally, the target class can be partially mocked in order to further isolate behavior.)  We then retrieve the class under test (in this case, CoffeeMachine) and its dependencies (IGrinder and IHopper) from the container for use.</p>
<p>If you do not need to setup any expectations in the specification, there is no need to retrieve the dependency from the container.  However, in this specification, we need both dependencies.</p>
<p><em>(Incidentally, I do not like the ClassUnderTest name.  I may end up changing this to Instance, ClassInstance, TargetClass, or something similar.  Suggestions?)</em></p>
<p>By moving the initialization of the RhinoAutoMocker to a base class, some of the chattiness of the context can be hidden.</p>
<ul>
<pre class="brush: csharp;">[Subject(typeof(CoffeeMachine),"using RhinoAutoMocker base")]
public class Example03_when_preparing_coffee_grounds : with_rhinoautomocker
{
    Establish context = () =&gt; Hopper.Expect(x =&gt; x.HasBeans()).Return(true);

    Because of = () =&gt; CoffeeMachine.ClassUnderTest.PrepareCoffeeGrounds(12);

    It should_check_if_hopper_has_beans = () =&gt; Hopper.VerifyAllExpectations();
    It should_grind_coffee = () =&gt; Grinder.AssertWasCalled(x =&gt; x.Grind(12));

}

[Subject("using RhinoAutoMocker&lt;T&gt;")]
public class with_rhinoautomocker
{
    protected static IGrinder Grinder;
    protected static IHopper Hopper;
    protected static RhinoAutoMocker&lt;CoffeeMachine&gt; CoffeeMachine;

    Establish context = () =&gt;
    {
        CoffeeMachine = new RhinoAutoMocker&lt;CoffeeMachine&gt;();
        Grinder = CoffeeMachine.Get&lt;IGrinder&gt;();
        Hopper = CoffeeMachine.Get&lt;IHopper&gt;();
    };
}</pre>
<p>However, we are still initializing and accessing the class in a less than optimal manner.To make the usage of the container a little more seamless, I decided to create a wrapper and factory for the AutoMocker.  Setup of the dependencies and expectations are done through an AutoMocker wrapper object.  The instance of the class under test is accessed through a ClassUnderTest object.</p>
<h4>The Result</h4>
<p>The specification (and base class) now look like this:</p>
<pre class="brush: csharp;">[Subject(typeof(CoffeeMachine),"using coffee machine base")]
public class Example05_when_preparing_coffee_grounds : with_coffee_machine
{
    Establish context = () =&gt; Hopper.Expect(x =&gt; x.HasBeans()).Return(true);

    Because of = () =&gt; ClassUnderTest.PrepareCoffeeGrounds(12);

    It should_check_if_hopper_has_beans = () =&gt; Hopper.VerifyAllExpectations();
    It should_grind_coffee = () =&gt; Grinder.AssertWasCalled(x =&gt; x.Grind(12));
}

[Subject("using SpecificationFor&lt;CoffeeMachine&gt;")]
public class with_coffee_machine : SpecificationFor&lt;CoffeeMachine&gt;
{
    protected static IGrinder Grinder;
    protected static IHopper Hopper;

    public with_coffee_machine
    {
        Grinder = AutoMocker.Get&lt;IGrinder&gt;();
        Hopper = AutoMocker.Get&lt;IHopper&gt;();
    }
}</pre>
<p>As you can see, the base class inherits from SpecificationFor&lt;T&gt;.  The default constructor uses Rhino.Mocks(MockMode.AAA) to generate the dependencies.  It then exposes a AutoMocker object, which is just the wrapper around the AutoMocker base class.  It also exposes a ClassUnderTest object.  If I didn’t need to use the dependencies, I could make the <em>Example05_when_preparing_coffee_grounds</em> class inherit from <em>SpecificationFor&lt;CoffeeMachine&gt; </em>instead of inheriting from the base class<em>.</em></p>
<h4>The Code</h4>
<p>I have included the code for the wrapper below for your review.  The code and six examples are available on my <a href="http://codeprogression.googlecode.com/svn/trunk/CodeProgression.Framework.Testing" target="_blank">Google code repository</a>. The repository also includes an example using the MoqAutoMocker.  All the examples have the same assertions, but are built up using the different techniques.</p>
<pre class="brush: csharp;">namespace CodeProgression.Framework.Testing
{
    public abstract class SpecificationFor&lt;T&gt; where T: class
    {
        protected static ClassUnderTest&lt;T&gt; Factory;

        // UPDATE 2009-12-21:
        //   Moved initialization here
        protected static T ClassUnderTest {get {return AutoMocker.Instance;} }

        protected SpecificationFor()
        {
            AutoMocker = AutoMockFactory.CreateTarget&lt;T&gt;();

            // UPDATE 2009-12-21:
            //    Initializing here prevented injected dependencies!
            // AutoMocker.PartialMockTheClassUnderTest();
            // ClassUnderTest = AutoMocker.Instance;
        }

        protected SpecificationFor(AutoMockType type)
        {
            AutoMocker = AutoMockFactory.CreateTarget&lt;T&gt;(type);

            // UPDATE 2009-12-21:
            // Initializing here prevented injected dependencies!
            // AutoMocker.PartialMockTheClassUnderTest();
            // ClassUnderTest = AutoMocker.Instance;
        }
    }

    public static class AutoMockFactory
    {
        public static ClassUnderTest&lt;TARGETCLASS&gt; CreateTarget&lt;TARGETCLASS&gt;() where TARGETCLASS : class
        {
            return CreateTarget&lt;TARGETCLASS&gt;(AutoMockType.RhinoMocksAAA);
        }

        public static ClassUnderTest&lt;TARGETCLASS&gt; CreateTarget&lt;TARGETCLASS&gt;(AutoMockType framework) where TARGETCLASS : class
        {
            AutoMocker&lt;TARGETCLASS&gt; mocker;
            ServiceLocator serviceLocator;
            switch (framework)
            {
                case AutoMockType.RhinoMocksAAA:
                    mocker = new RhinoAutoMocker&lt;TARGETCLASS&gt;(MockMode.AAA);
                    serviceLocator = new RhinoMocksAAAServiceLocator();
                    break;
                case AutoMockType.RhinoMocksClassic:
                    mocker = new RhinoAutoMocker&lt;TARGETCLASS&gt;(MockMode.RecordAndReplay);
                    serviceLocator = new RhinoMocksClassicServiceLocator();
                    break;
                case AutoMockType.Moq:
                    mocker = new MoqAutoMocker&lt;TARGETCLASS&gt;();
                    serviceLocator = new MoqServiceLocator();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("framework");
            }
            return new ClassUnderTest&lt;TARGETCLASS&gt;(mocker, serviceLocator);
        }
    }

    public enum AutoMockType
    {
        Moq,
        RhinoMocksAAA,
        RhinoMocksClassic
    }

    public class ClassUnderTest&lt;TARGETCLASS&gt; where TARGETCLASS : class
    {
        readonly IAutoMocker&lt;TARGETCLASS&gt; _mocker;
        readonly ServiceLocator _serviceLocator;

        public ClassUnderTest(IAutoMocker&lt;TARGETCLASS&gt; mocker, ServiceLocator serviceLocator)
        {
            _mocker = mocker;
            _serviceLocator = serviceLocator;
        }

        public AutoMockedContainer Container
        {
            get { return _mocker.Container; }
        }

        public TARGETCLASS Instance
        {
            get { return _mocker.ClassUnderTest; }
        }

        public void MockObjectFactory()
        {
            _mocker.MockObjectFactory();
        }

        public void PartialMockTheClassUnderTest()
        {
            _mocker.PartialMockTheClassUnderTest();
        }

        public T Get&lt;T&gt;() where T : class
        {
            return _mocker.Get&lt;T&gt;();
        }

        public void Inject(Type pluginType, object stub)
        {
            _mocker.Inject(pluginType, stub);
        }

        public void Inject&lt;T&gt;(T target)
        {
            _mocker.Inject(target);
        }

        public T AddAdditionalMockFor&lt;T&gt;() where T : class
        {
            return _mocker.AddAdditionalMockFor&lt;T&gt;();
        }

        public void UseConcreteClassFor&lt;T&gt;()
        {
            _mocker.UseConcreteClassFor&lt;T&gt;();
        }

        public T[] CreateMockArrayFor&lt;T&gt;(int count) where T : class
        {
            return _mocker.CreateMockArrayFor&lt;T&gt;(count);
        }

        public void InjectArray&lt;T&gt;(T[] stubs)
        {
            _mocker.InjectArray(stubs);
        }

        public T Mock&lt;T&gt;() where T : class
        {
            return _serviceLocator.Service&lt;T&gt;();
        }
        public object Mock(Type serviceType)
        {
            return _serviceLocator.Service(serviceType);
        }
        public T PartialMock&lt;T&gt;() where T : class
        {
            return _serviceLocator.PartialMock&lt;T&gt;();
        }
    }
}</pre>
</ul>
<p><script type="text/javascript">// <![CDATA[
 var cirerolGaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + cirerolGaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
// ]]&gt;</script></p>
<p><script type="text/javascript">// <![CDATA[
 var cirerolPageTracker = _gat._getTracker('UA-8257866-3'); cirerolPageTracker._initData(); cirerolPageTracker._trackPageview();
// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/11/30/wrapping-up-the-structuremap-automocking-container/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MOQ Mothers</title>
		<link>http://elegantcode.com/2009/07/14/moq-mothers/</link>
		<comments>http://elegantcode.com/2009/07/14/moq-mothers/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 05:37:18 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/07/14/moq-mothers/</guid>
		<description><![CDATA[Am I the only one who does this?
I like to use Object Mothers in my test projects, especially when I have a large number of services or entities I need to feed into the things I am testing. For example, if I have a class I am testing that takes a constructor argument, I may [...]]]></description>
			<content:encoded><![CDATA[<p>Am I the only one who does this?</p>
<p>I like to use <a href="http://martinfowler.com/bliki/ObjectMother.html" target="_blank">Object Mothers</a> in my test projects, especially when I have a large number of services or entities I need to feed into the things I am testing. For example, if I have a class I am testing that takes a constructor argument, I may set it up in my test like this:</p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Arrange()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     _recipeController = <span style="color: #0000ff">new</span> RecipeController(<span style="color: #0000ff">new</span> RecipeEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>
  </p>
</p>
<p>Since I probably don’t want to pass in a real RecipeEntity I can obviously mock one. (Also, don’t freak over the fact that I am injecting a single entity into a controller. This isn’t the point of the post.)</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Arrange()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     Mock&lt;IRecipeEntity&gt; moqRm = <span style="color: #0000ff">new</span> Mock&lt;IRecipeEntity&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     _recipeController = <span style="color: #0000ff">new</span> RecipeController(moqRm.Object);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>And this is all old hat goodness. It turns out that I find I want to mock IRecipeEntity a lot. And because of that, I can put it up in my context class OR I can get the mock through an object mother. </p>
<p>This is especially useful when I need to build up a complex set of dependencies. So without further delay, here is a mother implementation that mocks up a complete entity.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> DomainEntityMother</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>    {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>        <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IRecipeEntity Moq_IRecipeEntity()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>            Guid id = Guid.NewGuid();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>            var moq = <span style="color: #0000ff">new</span> Mock&lt;IRecipeEntity&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>                moq.SetupGet(m =&gt; m.Id).Returns(id);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>                moq.SetupGet(m =&gt; m.Title).Returns(<span style="color: #006080">&quot;Recipe : &quot;</span> + id);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>                moq.SetupGet(m =&gt; m.CooksNotes).Returns(<span style="color: #006080">&quot;Notes : &quot;</span> + id);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>                moq.SetupGet(m =&gt; m.Ingredients).Returns(DomainEntityMother.Moq_IIngredientEntities());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>            <span style="color: #0000ff">return</span> moq.Object;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>        <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IList&lt;IIngredientEntity&gt; Moq_IIngredientEntities()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>            var ingredientEntities = <span style="color: #0000ff">new</span> List&lt;IIngredientEntity&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>                ingredientEntities.Add(Moq_IIngredientEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>                ingredientEntities.Add(Moq_IIngredientEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>                ingredientEntities.Add(Moq_IIngredientEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>                ingredientEntities.Add(Moq_IIngredientEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>            <span style="color: #0000ff">return</span> ingredientEntities;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> ingredientCounter = 0;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>        <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IIngredientEntity Moq_IIngredientEntity()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>            ingredientCounter++;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>            Guid id = Guid.NewGuid();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>            var moq = <span style="color: #0000ff">new</span> Mock&lt;IIngredientEntity&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>            moq.SetupGet(m =&gt; m.Amount).Returns(ingredientCounter.ToString);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>            moq.SetupGet(m =&gt; m.Unit).Returns(<span style="color: #006080">&quot;TBSP&quot;</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>            moq.SetupGet(m =&gt; m.Description).Returns(<span style="color: #006080">&quot;Ingredient : &quot;</span> + id);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>            moq.SetupGet(m =&gt; m.Id).Returns(id);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>            <span style="color: #0000ff">return</span> moq.Object;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum42">  42:</span>        <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IList&lt;IRecipeEntity&gt; Moq_IRecipeEntities()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum43">  43:</span>        {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum44">  44:</span>            IList&lt;IRecipeEntity&gt; recipeEntities = <span style="color: #0000ff">new</span> List&lt;IRecipeEntity&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum45">  45:</span>            <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; 5; i++)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum46">  46:</span>            {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum47">  47:</span>                recipeEntities.Add(Moq_IRecipeEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum48">  48:</span>            }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum49">  49:</span>            <span style="color: #0000ff">return</span> recipeEntities;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum50">  50:</span>        }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum51">  51:</span>    }</pre>
<p><!--CRLF--></div>
</div>
</p>
<p>And now my test reads like this:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Arrange()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     _recipeController = <span style="color: #0000ff">new</span> RecipeController(DomainEntityMother.Moq_IRecipeEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now, this is cool, but what if I want to make a bad entity? One that should make my test fail, for instance? Do I need another mother? Nope, I just use the one the mother created, an mess with it a little.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Arrange()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     var moqEntity = Mock.Get(DomainEntityMother.Moq_IRecipeEntity());</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>         moqEntity.SetupGet(m =&gt; m.Id).Returns(Guid.Empty);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     _recipeController = <span style="color: #0000ff">new</span> RecipeController(moqEntity.Object);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now I can create all those boundary condition tests that SHOULD fail by starting with a good entity and then only changing the parts I want to cause to be invalid.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/07/14/moq-mothers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
