<?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>Musings&#60;Biefeld&#62; &#187; Unit Testing</title>
	<atom:link href="http://sbiefeld.com/category/coding/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://sbiefeld.com</link>
	<description>- curiosities of development, life, the universe and everything -</description>
	<lastBuildDate>Thu, 17 Nov 2011 04:01:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Step by Step to Using MSpec (Machine.Specifications) with ReSharper</title>
		<link>http://sbiefeld.com/2009/08/step-by-step-to-using-mspec-machine-specifications-with-resharper/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=step-by-step-to-using-mspec-machine-specifications-with-resharper</link>
		<comments>http://sbiefeld.com/2009/08/step-by-step-to-using-mspec-machine-specifications-with-resharper/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 20:33:52 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[Behavior Driven Design]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[MSpec]]></category>
		<category><![CDATA[ReSharper]]></category>
		<category><![CDATA[Specifications]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=391</guid>
		<description><![CDATA[Whilst researching using MSpec with ReSharper I found it difficult to find all the resources I needed in one place. This is an attempt to condense everything into that one place and facilitate those seeking to accomplish the same task. Step 1 &#8211; Git It: First thing&#8217;s first, grab the latest Machine Spec source from [...]]]></description>
			<content:encoded><![CDATA[<p><br/>
<p>Whilst researching using MSpec with ReSharper I found it difficult to find all the resources I needed in one place. This is an attempt to condense everything into that one place and facilitate those seeking to accomplish the same task.</p>
<h3>Step 1 &#8211; Git It:</h3>
<p>First thing&#8217;s first, grab the latest Machine Spec source from github.</p>
<pre style="background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #BEBEC8; font-size: 10pt;">$ git clone git://github.com/machine.machine.specifications.git mspec</pre>
<h3>Step 2 &#8211; Build It:</h3>
<p>Next, open it up in Visual Studio, set it to build in release mode and build it. Now the binaries will be ready for you.
</p>
<h3>Step 3 &#8211; Setup ReSharper:</h3>
<p>Now we need to setup ReSharper to be able to utilize the MSpec framework and run the tests in ReSharper&#8217;s test runner. To do this we need to add a plugins directory to the &#8220;JetBrains\ReSharper\v4.5\Bin&#8221; directory or the where ever the bin directory for your ReSharper is located. In the Plugins create a Machine.Specifications directory, so you should now have a path similar to; &#8220;JetBrains\ReSharper\v4.5\Bin\Plugins\Machine.Specifications&#8221;. Place the following dlls in the newly created folder: Machine.Specifications.ReSharperRunner.4.5.dll and Machine.Specifications.dll.
</p>
<h3>Step 4 &#8211; Write some Specifications:</h3>
<p>Coolio, now to test some behaviors, the dlls needed in our test project; Machine.Specifications.dll, Machine.Specifications.NUnit.dll or Machine.Specifications.XUnit.dll, and the appropriate test framework dll.</p>
<p><br/>
<p>Let&#8217;s take a look at a couple of examples to get used to the syntax. The most common keywords you want to pay attention to are <strong>Subject, Establish, Because</strong> and <strong>It</strong>. Declare the Subject of your Spec, Establish a context of the spec, Because x occurs, It should do something. For more complex scenarios you can use the keyword, <strong>Behaves_like</strong> and the <strong>Behaviors</strong> attribute which allows you to define complex behaviors. If you need to perform some cleanup use the <strong>Cleanup</strong> keyword.</p>
<p>Now for a couple of simple contrived examples&#8230;</p>
<p>This first specification looks at adding two numbers:</p>
<pre style="background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #BEBEC8; font-size: 10pt;">[<span style="color: #7386a5;">Subject</span>(<span style="color: #8f9d6a;">"adding two operands"</span>)]
<span style="color: #cda869;">public class</span> <span style="color: #7386a5;">when_adding_two_operands</span>
{
	<span style="color: #cda869;">static decimal</span> value;

	<span style="color: #7386a5;">Establish</span> context <span style="color: #cda869;">=</span> () <span style="color: #cda869;">=&gt;</span>
		value <span style="color: #cda869;">=</span> <span style="color: #9B7032;">0m</span>;

	<span style="color: #7386a5;">Because</span> of <span style="color: #cda869;">=</span> () <span style="color: #cda869;">=&gt;</span>
		value <span style="color: #cda869;">= new</span> Operator().Add(<span style="color: #9B7032;">42.0m</span>, <span style="color: #9B7032;">42.0m</span>);

	<span style="color: #7386a5;">It</span> should_add_both_operands <span style="color: #cda869;">=</span> () <span style="color: #cda869;">=&gt;</span>
		value.ShouldEqual(<span style="color: #9B7032;">84.0m</span>);
}</pre>
<p>The second specification looks at adding multiple numbers:</p>
<pre style="background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #BEBEC8; font-size: 10pt;">[<span style="color: #7386a5;">Subject</span>(<span style="color: #8f9d6a;">"adding multiple operands"</span>)]
<span style="color: #cda869;">public class</span> <span style="color: #7386a5;">when_adding_multiple_operands</span>
{
	<span style="color: #cda869;">static decimal</span> value;

	<span style="color: #7386a5;">Establish</span> context <span style="color: #cda869;">=</span> () <span style="color: #cda869;">=&gt;</span>
		value <span style="color: #cda869;">=</span> <span style="color: #9B7032;">0m</span>;

	<span style="color: #7386a5;">Because</span> of <span style="color: #cda869;">=</span> () <span style="color: #cda869;">=&gt;</span>
		value <span style="color: #cda869;">= new</span> Operator().Add(<span style="color: #9B7032;">42m</span>, <span style="color: #9B7032;">42m</span>, <span style="color: #9B7032;">42m</span>);

	<span style="color: #7386a5;">It</span> should_add_all_operands <span style="color: #cda869;">=</span> () <span style="color: #cda869;">=&gt;</span>
		value.ShouldEqual(<span style="color: #9B7032;">126m</span>);
}</pre>
<p>The code being tested:</p>
<pre style="background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #BEBEC8; font-size: 10pt;"><span style="color: #cda869;">public class</span> <span style="color: #7386a5;">Operator</span>
{
	<span style="color: #cda869;">public decimal</span> Add(<span style="color: #cda869;">decimal</span> firstOperand, <span style="color: #cda869;">decimal</span> secondOperand)
	{
		<span style="color: #cda869;">return</span> firstOperand <span style="color: #cda869;">+</span> secondOperand;
	}

	<span style="color: #cda869;">public decimal</span> Add(<span style="color: #cda869;">params decimal</span>[] operands)
	{
		<span style="color: #cda869;">decimal</span> value <span style="color: #cda869;">=</span> <span style="color: #9B7032;">0m</span>;

		<span style="color: #cda869;">foreach</span> (<span style="color: #cda869;">var</span> operand <span style="color: #cda869;">in</span> operands)
		{
			value <span style="color: #cda869;">+=</span> operand;
		}

		<span style="color: #cda869;">return</span> value;
	}
}</pre>
<h3>Step 5 &#8211; Create Templates to Improve Your Efficiency:</h3>
<p>
Using ReSharper templates is a good way to improve your spec writing efficiency, the following are templates I have been using.</p>
<p>This first one is for your normal behaviors:</p>
<pre style="background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #BEBEC8; font-size: 10pt;">[Subject("$subject$")]
public class when_<span style="color: #cda869;">$when$</span>
{
	Establish context =()=&gt;	{};

	Because of =()=&gt; {};		

	It should_$should$ =()=&gt; <span style="color: #7386a5;">$END$</span>;
}</pre>
<p>This one&#8217;s for assertions by themselves:</p>
<pre style="background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #BEBEC8; font-size: 10pt;">It should_<span style="color: #cda869;">$should$</span> =()=&gt; <span style="color: #7386a5;">$END$</span>;</pre>
<h3>Step 6 &#8211; Run the Report</h3>
<p>The report generated is pretty much the exact same as the SpecUnit report. The easiest thing to do is place the following MSpec binaries and file in the directory where your test binaries are placed; CommandLine.dll, Machine.Specifications.ConsoleRunner.exe, Machine.Specifications.dll and CommandLine.xml. The command to run the report goes a little something like:</p>
<pre style="background-color: #141414;font-family: Lucida Console;padding: 5px;border:solid 1px #333; overflow: auto; color: #BEBEC8; font-size: 10pt;">machine.specifications.consolerunner --html &lt;the location you want the html report stored&gt; &lt;your test dll name&gt;</pre>
<p>This is the report generated from the example specs:<br/><br/><img src="http://sbiefeld.com/wp-content/uploads/2009/08/MSpecReportExample.PNG" alt="MSpecReportExample" title="MSpecReportExample" width="434" height="390" class="alignnone size-full wp-image-394" style="border: solid 1px #ddd;" /></p>
<p><br/>
<p>Well, that&#8217;s about all for now, let me know if you have any questions.</p>
<p><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2009/08/step-by-step-to-using-mspec-machine-specifications-with-resharper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making c# lambda expressions more readable</title>
		<link>http://sbiefeld.com/2009/02/making-c-lambda-expressions-more-readable/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-c-lambda-expressions-more-readable</link>
		<comments>http://sbiefeld.com/2009/02/making-c-lambda-expressions-more-readable/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 23:44:02 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[Rhino Mocks]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=17</guid>
		<description><![CDATA[How often do you use lambda expressions?  I use them a great deal, mostly when I am making method assertions in Rhino Mocks.  If you do the bare minimum, which i see a lot, the expression can be somewhat cryptic. Less readable: Users.Find(x =&#62; x.Id == selectedUserId) I am guilty of doing this as well, [...]]]></description>
			<content:encoded><![CDATA[<p>How often do you use lambda expressions?  I use them a great deal, mostly when I am making method assertions in <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a>.  If you do the bare minimum, which i see a lot, the expression can be somewhat cryptic.<br />
<br />
Less readable:</p>
<pre class="csharpcode">Users.Find(x =&gt; x.Id == selectedUserId)</pre>
<p>I am guilty of doing this as well, without even realizing.  Maybe I am just being nit picky.<br />
<br />
I think it is much more readable if you use something more descriptive than some arbitrary letter in the alphabet.<br />
<br />
More readable:<br />
</p>
<pre class="csharpcode">Users.Find(user =&gt; user.Id == selectedUserId)</pre>
<p>This becomes much more useful when you are coding more complex lambda expressions.  One example is when making a method assertion using <a href="http://ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a>.<br />
<br />
Less readable:</p>
<pre class="csharpcode">_userRepository.AssertWasCalled
(
     x =&gt; x.Save(newUser),
     o =&gt; o.IgnoreArguments()
);</pre>
<p>With that assertion we have two arbitrary letters, what the heck does ‘x’ and  ‘o’ represent.  Are we talking about hugs and kisses.  I don’t think so.<br />
<br />
So to remedy this, lets change ‘x’ to ‘userRepository’ and ‘o’ to ‘method assertion’.  I believe these terms will make the assertion much more readable and concise.<br />
<br />
More readable:</p>
<pre class="csharpcode">_userRepository.AssertWasCalled
(
     userRepository =&gt; userRepository.Save(newUser),
     methodAssertion =&gt; methodAssertion.IgnoreArguments()
);</pre>
<p>With that little change it is much easier to understand what is being asserted and what parameters are being set on that assertion.<br />
<br />
The hardest part is breaking the habit of using arbitrary letters.  In the long run a more descriptive expression improves the readability of the code.  It will also decrease the amount of time it takes a new person to understand the lambda expressions in the code base.</p>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2009/02/making-c-lambda-expressions-more-readable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

