<?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>rob bowley &#187; unit testing</title>
	<atom:link href="http://blog.robbowley.net/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.robbowley.net</link>
	<description>adventures in extreme programming</description>
	<lastBuildDate>Thu, 15 Jul 2010 08:12:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to initialise a class without calling the constructor (.Net)</title>
		<link>http://blog.robbowley.net/2009/08/27/how-to-initialise-a-class-without-calling-the-constructor-net/</link>
		<comments>http://blog.robbowley.net/2009/08/27/how-to-initialise-a-class-without-calling-the-constructor-net/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 16:16:04 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[legacy code]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.robbowley.net/?p=809</guid>
		<description><![CDATA[Sometimes we want to test some really nasty legacy code but are inhibited by constructors taking tricky things like HttpWhatevers, God objects and so on which you do not care about but would require enormous effort setting up just to try and get an instance of the damn thing so you can test your method.
One [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we want to test some really nasty legacy code but are inhibited by constructors taking tricky things like HttpWhatevers, God objects and so on which you do not care about but would require enormous effort setting up just to try and get an instance of the damn thing so you can test your method.</p>
<p>One way out is to create a parameterless constructor on the class which is only used for testing. Not at all nice, but sometimes necessary to create that first <a href="http://www.informit.com/articles/article.aspx?p=359417&amp;seqNum=2">seam</a>.</p>
<p>A candidate I was pair interviewing with introduced me to something which may prove preferable in these cases &#8211; the Microsoft serialization library has a method which will initialize a class without calling the constructor:</p>
<p><strong>FormatterServices.GetSafeUninitializedObject</strong><br />
<a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices.getsafeuninitializedobject.aspx">http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices.getsafeuninitializedobject.aspx</a></p>
<p>This way you don&#8217;t have to modify the code!</p>
<p>I would only ever advise using this if your only other sensible option would be to override the constructor. Hopefully once you have your tests you would be able to confidently refactor out the problematic code.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2009/08/27/how-to-initialise-a-class-without-calling-the-constructor-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thoughts on testing .Net code with RSpec, Cucumber &amp; IronRuby</title>
		<link>http://blog.robbowley.net/2009/02/22/thoughts-on-testing-net-code-with-rspec-ironruby/</link>
		<comments>http://blog.robbowley.net/2009/02/22/thoughts-on-testing-net-code-with-rspec-ironruby/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 17:20:27 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[ironruby]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.robbowley.net/?p=256</guid>
		<description><![CDATA[In a previous article I said I&#8217;d like to test my .Net code using RSpec so I&#8217;ve spent some time this weekend having a go at just that. IronRuby is still at alpha so was pretty flaky and I spent a lot of time hacking IronRuby and RSpec, but satisfyingly I eventually got it all [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://blog.robbowley.net/2009/02/04/bdd-syntax/">previous article</a> I said I&#8217;d like to test my .Net code using <a href="http://rspec.info/">RSpec</a> so I&#8217;ve spent some time this weekend having a go at just that. <a href="http://www.ironruby.net/">IronRuby</a> is still at alpha so was pretty flaky and I spent a lot of time hacking IronRuby and RSpec, but satisfyingly I eventually got it all working and did a few proof of concept examples. Surprisingly or not, there&#8217;s quite a few people out there with the same idea so if you want to have a go you wouldn&#8217;t go too far wrong to look at some of the examples I found (although none of them worked for me straight out of the box):</p>
<p><a href="http://msdn.microsoft.com/en-us/magazine/dd434651.aspx">Getting Started With IronRuby And RSpec</a><br />
<a href="http://www.patrickgannon.net/archive/2008/10/23/bdd-style-feature-tests-using-ironruby-and-rspeccucumber.aspx">BDD-style feature tests using IronRuby and RSpec/Cucumber</a><br />
<a href="http://khebbie.dk/post/2008/08/27/Example-of-using-ironRubys-mini_rspec-library.aspx">Example of using ironRubys mini_rspec library</a></p>
<p>Yeah, so game on I think. If you&#8217;re not sure why I&#8217;m so excited by the prospect of doing this I suggest you have a look at some <a href="http://www.code-magazine.com/article.aspx?quickid=0805061&#038;page=1">BDD</a> examples using <a href="http://rspec.info/">RSpec</a> and <a href="http://cukes.info/">Cucumber</a>. In my mind it&#8217;s light years ahead of what we can currently do with the .Net framework. </p>
<p>Whilst I really like the idea of actually doing this I do have some reservations:</p>
<ul>
<li>You still have to compile your .Net code so no enormous time savings.
</li>
<li>RSpec takes some time to get going as well, which is not a problem if you&#8217;re doing a large run but might be quite annoying otherwise.
</li>
<li>Have no idea how you&#8217;d be able to get test coverage reports (I&#8217;m sure some bright spark will get this working soon enough).</li>
<li>Not exactly sure how debugging would work (stepping into your .Net code from an RSpec test).
</ul>
<p>On a side note and forgetting about the instability of it for a second I was really impressed with IronRuby and the CLR interop so, er.. *gulp* well done Microsoft!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2009/02/22/thoughts-on-testing-net-code-with-rspec-ironruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Behaviour Driven Development syntax</title>
		<link>http://blog.robbowley.net/2009/02/04/bdd-syntax/</link>
		<comments>http://blog.robbowley.net/2009/02/04/bdd-syntax/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 21:51:06 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.robbowley.net/?p=185</guid>
		<description><![CDATA[I&#8217;ve been coding in the Behaviour Driven Development (BDD) style for a while now and thought I&#8217;d share my syntax at the state it has currently evolved to. Briefly, the idea behind of BDD is to have clear definition of business intent within your code, particularly your unit tests. The benefits are it&#8217;s easier to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been coding in the <a href="http://dannorth.net/introducing-bdd">Behaviour Driven Development</a> (BDD) style for a while now and thought I&#8217;d share my syntax at the state it has currently evolved to. Briefly, the idea behind of BDD is to have clear definition of business intent within your code, particularly your unit tests. The benefits are it&#8217;s easier to see what your system is supposed to be doing and your tests are more closely associated to the required behaviour of the system rather than the implementation, ergo, behaviour driven.</p>
<p>I originally started by making an effort to write my tests method names as sentences starting with &#8220;should&#8221; (e.g. should_set_active_status_to_false_when_status_updated), but having used the &#8220;Given, When, Then&#8221; format for our automated acceptance tests I&#8217;ve been trying to move more in this direction.</p>
<p>For a start, I&#8217;ve being using the Visual Studio solution explorer to help. Previously tests would be organised something like this:</p>
<p><img class="size-full wp-image-192" title="nonbdd_explorer" src="http://blog.robbowley.net/wp-content/uploads/2009/02/nonbdd_explorer.gif" alt="tests organised in explorer non bdd style" width="193" height="97" /></p>
<p>Which I have replaced with this:</p>
<p><img class="size-full wp-image-190" title="bdd_explorer" src="http://blog.robbowley.net/wp-content/uploads/2009/02/bdd_explorer.gif" alt="using solution explorer to help describe tests" width="332" height="99" /></p>
<p>As you can see I&#8217;m using folders and file names to help give the tests context. One major advantage of this is as the tests are less coupled to the implementation of the system under test (SUT) any changes to the implementation are less likely to require moving tests around so the intent remains intact. The other obvious advantage is the intended behaviour is much clearer.</p>
<p>The next thing to show you is the tests:</p>
<p><img class="alignnone size-full wp-image-206" title="bdd tests example 1" src="http://blog.robbowley.net/wp-content/uploads/2009/02/tests12.gif" alt="" width="575" height="266" /></p>
<p>The nice thing here is the namespace is taken from the folder and the class name from the file name so I&#8217;m getting a nice convention (even if it is being obfuscated by C# scaffolding). In this example I&#8217;m still sticking to the &#8220;should&#8221; convention but below I went for something a bit different:</p>
<p><img class="alignnone size-full wp-image-209" title="bdd tests example 2" src="http://blog.robbowley.net/wp-content/uploads/2009/02/tests22.gif" alt="" width="575" height="176" /></p>
<p>So this is a little different because the conditions are more specific. There&#8217;s another class for non view direct users so I split them up, partly so the test method names needn&#8217;t be so long but mainly because it&#8217;s easier to understand what&#8217;s going on. Purists may feel uncomfortable with my relative lack of a standardised syntax, but the most important thing to me is it&#8217;s easy to see what&#8217;s going on and beyond that I don&#8217;t particularly care how you say it.</p>
<p>What this really is is a feeble attempt to mimic the elegance you get from <a href="http://rspec.info/">RSpec</a> (which we&#8217;ve been using along with <a href="http://cukes.info/">Cucumber</a> &amp; <a href="http://wtr.rubyforge.org/">Watir</a> to write our automated tests). I was thinking today whilst I was playing with this that I don&#8217;t just want to mimic RSpec I actually want <em>write </em>my unit tests using RSpec. Excitingly it looks like the <a href="http://www.codeplex.com/dlr">DLR </a>and <a href="http://www.ironruby.net/">IronRuby</a> will <a href="http://msdn.microsoft.com/en-us/magazine/dd434651.aspx">make this possible</a>. Up until now I&#8217;d been struggling to think of a really appropriate use of a dynamic language within the .Net context but not any more.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2009/02/04/bdd-syntax/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>TDD keeps you focused</title>
		<link>http://blog.robbowley.net/2008/09/27/tdd-keeps-you-focused/</link>
		<comments>http://blog.robbowley.net/2008/09/27/tdd-keeps-you-focused/#comments</comments>
		<pubDate>Sat, 27 Sep 2008 21:18:00 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://test.robbowley.net/?p=11</guid>
		<description><![CDATA[
One of the less stated benefits of doing TDD is that it concentrates your mind on the problem.
Recently I was looking at a section of code which was creating a Lucene search string from criteria passed in from the user. I needed to add some new functionality, but it was a bit higgledy-piggledy so following [...]]]></description>
			<content:encoded><![CDATA[<div class="entrybody">
<p>One of the less stated benefits of doing TDD is that it concentrates your mind on the problem.</p>
<p>Recently I was looking at a section of code which was creating a Lucene search string from criteria passed in from the user. I needed to add some new functionality, but it was a bit higgledy-piggledy so following the <a href="http://www.pragmaticprogrammer.com/the-pragmatic-programmer/extracts/software-entropy">broken windows</a> principle I decided to refactor it to the <a href="http://en.wikipedia.org/wiki/Builder_pattern">builder</a> pattern before I added my new feature.</p>
<p>I ran NCover on the code and it reported 70% coverage which considering what I knew about the history of the system was surprisingly high. I decided there was enough coverage, paired with a colleague and dived in. At first we were doing well. Our changes broke the tests a few times which reassured us about the coverage, but after a while it felt that we were losing a bit of steam. We&#8217;d done a lot of refactoring but it didn&#8217;t feel like we&#8217;d got very far.</p>
<p>Skip to a couple of days later &#8211; I&#8217;d not been able to touch the code and neither had my colleague who had now been taken off to deal with another project, so I pulled in the other dev on my team who is probably the most fanatical TDD nut I&#8217;ve ever met. Immediately he was uncomfortable, he&#8217;d quickly spotted the tests were really only integration tests (something I had put to the back of my mind) and got jittery as we continued from where I&#8217;d left off. I didn&#8217;t like to see him in so much pain so quickly relented and let him write new tests even though I felt it was just going to slow us down, maybe even to the point where we wouldn&#8217;t get it finished. However he assured me that although it felt slow now we&#8217;d be racing along soon enough.</p>
<p>Not only was he right about that, but as we were doing it the objective was so much clearer. Writing the new tests meant we were forced to really think about each move we made so our refactoring had clear direction. We spent about the same time working on the problem but probably made twice as much progress as my previous attempt <em>and</em> ended up with a proper unit test suite around the code.</p>
<p>Some day <a href="http://research.microsoft.com/SpecSharp/">soon</a> people are probably going to do away with the need to do TDD, which I think will be a shame as it never ceases to amaze me how many benefits it has.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2008/09/27/tdd-keeps-you-focused/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R.I.P NMock</title>
		<link>http://blog.robbowley.net/2008/08/16/rip-nmock/</link>
		<comments>http://blog.robbowley.net/2008/08/16/rip-nmock/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 21:38:42 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[nmock]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://test.robbowley.net/?p=37</guid>
		<description><![CDATA[After Mini Spa this year I was fortunate enough to get to chat to Nat Pryce in the bar. He mentioned some interesting projects he&#8217;s working on, such as something I think he called &#8220;Domainatrix&#8221; &#8211; a tool you can use to analyse your code to see if it is relevant to the domain. Very [...]]]></description>
			<content:encoded><![CDATA[<p>After <a href="http://bcs-spa.org/minispa2008.html">Mini Spa</a> this year I was fortunate enough to get to chat to Nat Pryce in the bar. He mentioned some interesting projects he&#8217;s working on, such as something I think he called &#8220;Domainatrix&#8221; &#8211; a tool you can use to analyse your code to see if it is relevant to the domain. Very nice chap.</p>
<p>Having used <a href="http://www.nmock.org/">NMock</a> (which he helped write) extensively I asked him if anyone was looking after it as there hasn&#8217;t been any visible activity for a while. It turns out that Thoughtworks took it over as of release 2.0 but have clearly decided not to support it anymore.</p>
<p>If you&#8217;re still using it I highly recommend you start looking for a new mocking tool ASAP.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2008/08/16/rip-nmock/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>nMock vs RhinoMocks</title>
		<link>http://blog.robbowley.net/2008/07/09/nmock-vs-rhinomocks/</link>
		<comments>http://blog.robbowley.net/2008/07/09/nmock-vs-rhinomocks/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 21:16:04 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[nmock]]></category>
		<category><![CDATA[rhinomocks]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://test.robbowley.net/?p=9</guid>
		<description><![CDATA[I&#8217;ve recently started using RhinoMocks instead of nMock, mainly because it&#8217;s strongly-typed. However, I&#8217;ve found a few other little treats:
Stubs
In nMock, if you want to stub some method calls on a mocked interface you have to do something like this:
Mockery mockery = new Mockery();
IService mockService = mockery.NewMock();
Stub.On(mockService).Method("MethodA");
Stub.On(mockService).Method("MethodB");
Stub.On(mockService).Method("MethodC");
...

Which is cumbersome and noisy. In RhinoMocks you can [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started using RhinoMocks instead of nMock, mainly because it&#8217;s strongly-typed. However, I&#8217;ve found a few other little treats:</p>
<h3>Stubs</h3>
<p>In nMock, if you want to stub some method calls on a mocked interface you have to do something like this:</p>
<pre><code>Mockery mockery = new Mockery();
IService mockService = mockery.NewMock();
Stub.On(mockService).Method("MethodA");
Stub.On(mockService).Method("MethodB");
Stub.On(mockService).Method("MethodC");
...
</code></pre>
<p>Which is cumbersome and noisy. In RhinoMocks you can do this:</p>
<pre><code>MockRepository Repository repository = new MockRepository();
IService serviceMock = repository.Stub();
...
</code></pre>
<p>&#8230;and RhinoMocks will ignore all calls to that interface. This is really nice as you generally only test the SUT&#8217;s interaction with one dependency at a time.</p>
<h3>Dynamic Mocks</h3>
<p>If you only want to test one interaction with a dependency and ignore all others you can create a dynamic mock.</p>
<pre><code>MockRepository Repository repository = new MockRepository();
repository .DynamicMock();
...</code></pre>
<p>All calls to the mocked dependency will be ignored unless they are explicitly expected (e.g. Expect.Call(mockService.MethodA)&#8230;..). This is the same as <em>not</em> saying mockery.VerifyAllExpectationsHaveBeenMet() in nMock. It&#8217;s always annoyed me that you have to remember to do this in nMock and I much prefer that the default for RhinoMocks is to fail when encountering an unexpected method call.</p>
<h3>Raising Events</h3>
<p>nMock does not natively support raising events, which is a pain, but there are <a href="http://blog.gravityfree.ca/2007/03/raising-events-in-nmock-20.html">ways around it</a> (I&#8217;ve extended his example to support custom EventArgs which you can download <a href="http://blog.robbowley.net/assets/2008/7/9/MockEvent_1.zip">here</a>). With RhinoMocks it&#8217;s much simpler. Rather than explaining it myself, check out J-P Boodhoo&#8217;s great example <a href="http://codebetter.com/blogs/jean-paul_boodhoo/archive/2007/05/07/raising-events-from-a-mock-using-rhino-mocks.aspx">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2008/07/09/nmock-vs-rhinomocks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>testing private methods</title>
		<link>http://blog.robbowley.net/2008/04/16/testing-private-methods/</link>
		<comments>http://blog.robbowley.net/2008/04/16/testing-private-methods/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 21:13:24 +0000</pubDate>
		<dc:creator>rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://test.robbowley.net/?p=6</guid>
		<description><![CDATA[I&#8217;ve being doing TDD for a year or so and the part I have struggled with the most is testing private methods. Should you test them at all? If so, how do you test them?
Michael Feathers&#8217; book &#8220;Working Effectively with Legacy Code&#8221; has this to say about it:
&#8220;This question comes up over and over again [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve being doing TDD for a year or so and the part I have struggled with the most is testing private methods. Should you test them at all? If so, how do you test them?</p>
<p>Michael Feathers&#8217; <a href="http://www.amazon.co.uk/Working-Effectively-Legacy-Robert-Martin/dp/0131177052/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1208333368&amp;sr=8-1">book</a> &#8220;Working Effectively with Legacy Code&#8221; has this to say about it:</p>
<p><em>&#8220;This question comes up over and over again from people new to unit testing: &#8220;How do I test private methods?&#8221;. Many people spend a lot of time trying to figure out how to get around this problem, but [...] the real answer is that if you have the urge to test a private method, the method shouldn&#8217;t be private; if making the method public bothers you, chances are, it is because it is part of a separate responsibility: it should be on another class.&#8221;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.robbowley.net/2008/04/16/testing-private-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
