<?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 Test</title>
	<atom:link href="http://sbiefeld.com/tag/unit-test/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>Testing with a Compiled Class that Doesn&#8217;t Implement an Interface &#8211; Adapter Pattern</title>
		<link>http://sbiefeld.com/2009/05/testing-with-a-compiled-class-that-doesnt-implement-an-interface-adapter-pattern/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-with-a-compiled-class-that-doesnt-implement-an-interface-adapter-pattern</link>
		<comments>http://sbiefeld.com/2009/05/testing-with-a-compiled-class-that-doesnt-implement-an-interface-adapter-pattern/#comments</comments>
		<pubDate>Wed, 13 May 2009 04:32:12 +0000</pubDate>
		<dc:creator>Sean Biefeld</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Adapter Pattern]]></category>
		<category><![CDATA[Compiled Class]]></category>
		<category><![CDATA[Implement Interface]]></category>
		<category><![CDATA[Specification]]></category>
		<category><![CDATA[Unit Test]]></category>

		<guid isPermaLink="false">http://sbiefeld.com/?p=288</guid>
		<description><![CDATA[*UPDATE: Per&#160;sean chambers, this is an example of the&#160;adapter pattern I recently ran into an issue where I needed to implement a simple email service to send users a randomly generated PIN when they are first entered into the system. To accomplish this I decided to just use the System.Net.Mail implementation.&#160; To create and send [...]]]></description>
			<content:encoded><![CDATA[<p><br/>
<p>*UPDATE: Per&nbsp;<a href="http://sbiefeld.com/members/schambers/default.aspx" target="_blank">sean chambers</a>, this is an example of the&nbsp;<a href="http://en.wikipedia.org/wiki/Adapter_pattern" target="_blank">adapter pattern</a></p>
<p><br/></p>
<p>I recently ran into an issue where I needed to implement a simple email service to send users a randomly generated PIN when they are first entered into the system. To accomplish this I decided to just use the <a target="_blank" title="system.net.mail" href="http://msdn.microsoft.com/en-us/library/system.net.mail.aspx">System.Net.Mail</a> implementation.&nbsp; To create and send an email you have to use the <a target="_blank" title="SmtpClient" href="http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx">SmtpClient</a> class which does not implement an interface. All I really wanted to test was that the Send() method was called, I did not want to write an integration test that actually sends an email.</p>
<p><br/></p>
<p>One way to work around this problem is to create an interface containing the elements you need to mock from the compiled class.&nbsp; After this, create your own class that inherits the compiled class and implements your interface. Now when testing, you can seemingly mock up the non-interfaced compiled class, which is exactly what I wanted to achieve. I am not sure whether this is the appropriate way to handle the issue, if anyone has any thoughts on a better way to do this, I would be grateful for the advice.</p>
<p>My specification ended up looking like this:</p>
<p><br/></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;">EmailServiceSpecs</span> : <span style="color: #7386a5;">ContextSpecification</span>
{
	<span style="color: #cda869;">protected</span> <span style="color: #d0da90;">IEmailService</span> _emailService;
	<span style="color: #cda869;">protected</span> <span style="color: #d0da90;">ISmtpClient</span> _smtpClient;
	<span style="color: #cda869;">protected string</span> _emailTo <span style="color: #cda869;">=</span> <span style="color: #8f9d6a;">"phillip.fry@planetexpress.com"</span>;
	<span style="color: #cda869;">protected string</span> _emailFrom <span style="color: #cda869;">=</span> <span style="color: #8f9d6a;">"hermes.conrad@planetexpress.com"</span>;
	<span style="color: #cda869;">protected string</span> _emailSubject <span style="color: #cda869;">=</span> <span style="color: #8f9d6a;">"New Process to Improve Morale"</span>;
	<span style="color: #cda869;">protected string</span> _emailBody <span style="color: #cda869;">=</span> <span style="color: #8f9d6a;">"From now on all employees will be required to have Brain slugs, remember, a mindless worker is a happy worker."</span>;

	<span style="color: #cda869;">protected override void</span> SharedContext()
	{
		<span style="color: #7386a5;">DependencyInjection</span><span style="color: #cda869;">.</span><span style="color: #7386a5;">RegisterType</span>&lt;<span style="color: #d0da90;">IEmailService</span>, <span style="color: #7386a5;">EmailService</span>&gt;();

		_emailService <span style="color: #cda869;">=</span> <span style="color: #7386a5;">DependencyInjection</span>
			<span style="color: #cda869;">.</span>GetDependency&lt;<span style="color: #d0da90;">IEmailService</span>&gt;(_emailTo, _emailFrom, _emailSubject, _emailBody);

		_smtpClient <span style="color: #cda869;">=</span> <span style="color: #7386a5;">MockRepository</span>.GenerateMock&lt;<span style="color: #d0da90;">ISmtpClient</span>&gt;();

		<span style="color: #7386a5;">DependencyInjection</span><span style="color: #cda869;">.</span>RegisterInstance(_smtpClient);
	}
}

[<span style="color: #7386a5;">TestFixture</span>]
[<span style="color: #7386a5;">Concern</span>(<span style="color: #8f9d6a;">"Email Service"</span>)]
<span style="color: #cda869;">public class</span> <span style="color: #7386a5;">when_sending_an_email</span> : <span style="color: #7386a5;">EmailServiceSpecs</span>
{
	<span style="color: #cda869;">protected override void</span> Context()
	{
		_smtpClient.Stub(smptClient <span style="color: #cda869;">=&gt;</span> smptClient<span style="color: #cda869;">.</span>Send(<span style="color: #cda869;">new</span> <span style="color: #7386a5;">MailMessage</span>()))
			<span style="color: #cda869;">.</span>IgnoreArguments()
			<span style="color: #cda869;">.</span>Repeat<span style="color: #cda869;">.</span>Any();

		_emailService<span style="color: #cda869;">.</span>Send();
	}

	[<span style="color: #7386a5;">Test</span>]
	[<span style="color: #7386a5;">Observation</span>]
	<span style="color: #cda869;">public void</span> should_send_email()
	{
		_smtpClient<span style="color: #cda869;">.</span>AssertWasCalled<
			(smtpClient <span style="color: #cda869;">=&gt;</span> smtpClient<span style="color: #cda869;">.</span>Send(<span style="color: #cda869;">new</span> MailMessage()),
			assertionOptions <span style="color: #cda869;">=&gt;</span> assertionOptions<span style="color: #cda869;">.</span>IgnoreArguments());
	}
}
</pre>
<p>Below are my email classes:</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 interface</span> <span style="color: #d0da90;">ISmtpClient</span>
{
	<span style="color: #cda869;">void</span> Send(<span style="color: #7386a5;">MailMessage</span> message);
}

[<span style="color: #7386a5;">MapDependency</span>(<span style="color: #cda869;">typeof</span>(<span style="color: #d0da90;">ISmtpClient</span>))]
<span style="color: #cda869;">public class </span><span style="color: #7386a5;">SubsideSmtpClient</span> : <span style="color: #7386a5;">SmtpClient</span>, <span style="color: #d0da90;">ISmtpClient</span> { }

<span style="color: #cda869;">public interface</span> <span style="color: #d0da90;">IEmailService</span>
{
	<span style="color: #cda869;">void</span> Send();
}

[<span style="color: #7386a5;">MapDependency</span>(<span style="color: #cda869;">typeof</span>(<span style="color: #d0da90;">IEmailService</span>))]
<span style="color: #cda869;">public class</span> <span style="color: #7386a5;">EmailService</span> : <span style="color: #d0da90;">IEmailService</span>
{
	<span style="color: #cda869;">public</span> <span style="color: #7386a5;">EmailService</span>(<span style="color: #cda869;">string</span> to, <span style="color: #cda869;">string</span> from, <span style="color: #cda869;">string</span> subject, <span style="color: #cda869;">string</span> body)
	{
		Email = <span style="color: #cda869;">new</span> <span style="color: #7386a5;">MailMessage</span>(from, to, subject, body);
	}

	<span style="color: #cda869;">protected</span> <span style="color: #7386a5;">MailMessage</span> Email
	{
		<span style="color: #cda869;">get</span>; <span style="color: #cda869;">set</span>;
	}

	<span style="color: #cda869;">private</span> <span style="color: #d0da90;">ISmtpClient</span> _smptClient;

	<span style="color: #cda869;">protected</span> <span style="color: #d0da90;">ISmtpClient</span> Smtp
	{
		<span style="color: #cda869;">get</span>
		{
			_smptClient = <span style="color: #7386a5;">DependencyUtilities</span>
				<span style="color: #cda869;">.</span>RetrieveDependency(_smptClient);
			<span style="color: #cda869;">return</span> _smptClient;
		}
	}

	<span style="color: #cda869;">public void</span> Send()
	{
		Smtp<span style="color: #cda869;">.</span>Send(Email);
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sbiefeld.com/2009/05/testing-with-a-compiled-class-that-doesnt-implement-an-interface-adapter-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

