<?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>Flexible Diamond &#187; agile</title>
	<atom:link href="http://blog.flexiblediamond.com/tag/agile/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexiblediamond.com</link>
	<description></description>
	<lastBuildDate>Wed, 02 Sep 2009 12:31:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What&#8217;s the point of all this mockery?</title>
		<link>http://blog.flexiblediamond.com/2009/08/whats-the-point-of-all-this-mockery/</link>
		<comments>http://blog.flexiblediamond.com/2009/08/whats-the-point-of-all-this-mockery/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 21:53:28 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=176</guid>
		<description><![CDATA[I&#8217;ve jumped in with both feet, without pausing to explain why I&#8217;m doing it &#8211; so, where do mock objects fit into my daily testing behaviours?

when the system under test relies on an expensive resource
when the system under test relies on a live resource
when the system under test relies on a resource that doesn&#8217;t yet [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve jumped in with both feet, without pausing to explain why I&#8217;m doing it &#8211; so, where do mock objects fit into my daily testing behaviours?</p>
<ul>
<li>when the system under test relies on an expensive resource</li>
<li>when the system under test relies on a live resource</li>
<li>when the system under test relies on a resource that doesn&#8217;t yet exist</li>
<li>when I want to ensure that the system under test uses the resource correctly</li>
</ul>
<p>I had to build a couple of client applications recently (twitter and flickr, since you ask &#8211; talk about re-inventing the wheel &#8230;), which interacted with what I consider expensive resources &#8211; firstly, I was contacting the remote flickr servers and awaiting their response each time I tested; secondly, the twitter API <a href="http://apiwiki.twitter.com/Rate-limiting">puts limits on how often it&#8217;s called</a>, which means that I could run out of calls pretty quickly if every test made at least one call, and all tests were run each time I compiled the movie.</p>
<p>A couple of years ago, we built a googlemaps-stylee flash app for a certain weekend-break company to allow users to choose chalets (/lodges/huts/accommodation units &#8211; bizarrely &#8216;accommodation units&#8217; was the official term) online. The units&#8217; availability was updated in real time, so the app had to query the server for the latest data. The only problem with this was that the server application was still under design &#8211; it didn&#8217;t yet exist.</p>
<p>I&#8217;ve also worked with database developers who haven&#8217;t yet finalised their DB structure, and who keep dropping and re-building the database, which means deleting all the data that my test scripts rely on.</p>
<p>You only need to experience difficulties such as these a couple of times to realise that it&#8217;s imperative to have control of the remote/live/non-existent/under-development resource in order for the tests to be of any use.</p>
<p>The good news is that &#8211; in most cases &#8211; it&#8217;s possible to define an API for communication with the resource even before it exists, and that&#8217;s what makes mock objects so useful &#8211; they can honour the API, thus enabling the code we build against the tests to work against the resource when it&#8217;s ready.</p>
<p>Strikes me an example might be useful. Twitter suit you?</p>
<p>I have a TwitterView object, which takes a TwitterProxy which in turn communicates with the Twitter servers. I know it&#8217;s terrible &#8211; aren&#8217;t these chaps supposed to be cleanly separated? The TwitterView receives an event when the proxy has done all its loading, and then can populate the relevant TextFields. Easy, innit.</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">class</span> TwitterView <span class="kw3">extends</span> Sprite <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> title:<span class="kw3">TextField</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> tweets:<span class="kw3">TextField</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> proxy:ITwitterProxy;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> TwitterView<span class="br0">&#40;</span>proxy:ITwitterProxy = <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">proxy</span> = proxy;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initView<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTitle<span class="br0">&#40;</span><span class="st0">&quot;Hello World!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> getTitle<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">String</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> title.<span class="kw3">text</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> setTitle<span class="br0">&#40;</span>title:<span class="kw3">String</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">title</span>.<span class="kw3">text</span> = title;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> setTweets<span class="br0">&#40;</span>tweets:<span class="kw3">Array</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">this</span>.<span class="me1">tweets</span>.<span class="kw3">htmlText</span> = tweets.<span class="kw3">join</span><span class="br0">&#40;</span><span class="st0">&quot;<span class="es0">\n</span><span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; override <span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">String</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&quot;[TwitterView]&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>proxy == <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proxy = <span class="kw2">new</span> TwitterProxy<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proxy.<span class="me1">addEventListener</span><span class="br0">&#40;</span>TwitterLoadEvent.<span class="me1">TWITTER_LOADED</span>, onTwitterLoaded<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proxy.<span class="me1">init</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> initView<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = <span class="kw2">new</span> <span class="kw3">TextField</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title.<span class="kw3">autoSize</span> = TextFieldAutoSize.<span class="kw3">LEFT</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>title<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tweets = <span class="kw2">new</span> <span class="kw3">TextField</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tweets.<span class="kw3">width</span> = <span class="nu0">500</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tweets.<span class="kw3">autoSize</span> = TextFieldAutoSize.<span class="kw3">LEFT</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tweets.<span class="kw3">wordWrap</span> = <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tweets.<span class="me1">y</span> = title.<span class="me1">y</span> + title.<span class="kw3">height</span> + <span class="nu0">10</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>tweets<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> onTwitterLoaded<span class="br0">&#40;</span>event:TwitterLoadEvent<span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTitle<span class="br0">&#40;</span>event.<span class="me1">proxy</span>.<span class="me1">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTweets<span class="br0">&#40;</span>event.<span class="me1">proxy</span>.<span class="me1">getTweets</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In the onTwitterLoaded function, you can see the two most important calls that we need to make on the TwitterProxy: getTitle() and getTweets(). I put these in the ITwitterProxy interface.</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw3">interface</span> ITwitterProxy <span class="kw3">extends</span> IEventDispatcher <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> getTweets<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Array</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> getTitle<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">String</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So now we have a fairly basic view, which expects its data from the proxy. </p>
<p>Let&#8217;s see it in action, courtesy of TwitterApp. Yep, not much happening:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">class</span> TwitterApp <span class="kw3">extends</span> Sprite <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> TwitterApp<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">function</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> view:TwitterView = <span class="kw2">new</span> TwitterView<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addChild<span class="br0">&#40;</span>view<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.<span class="me1">init</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>We want to practise test-driven development &#8211; write a failing test > code until it passes > write another test > refactor &#8211; so we want to write a unit test that will check the view&#8217;s title and tweets, but (a) we can&#8217;t rely on the live twitter data as it will change over time, and (b) we don&#8217;t want to run up against the limit of API calls that we can make. Time for a mock object, methinks.</p>
<p>Firstly, I&#8217;m using <a href="http://asunit.org">ASUnit</a> rather than FlexUnit, so if you&#8217;re following along, you&#8217;ll need to grab that and the <a href="http://blog.hoardinghopes.com/index.php/2009/07/mockito-flex-meets-asunit/">ASUnitMockitoTestCase bridging class</a> that I published one or two articles ago. Let&#8217;s start with a skeleton testcase:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">class</span> TwitterTest <span class="kw3">extends</span> ASUnitMockitoTestCase <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> TwitterTest<span class="br0">&#40;</span>testMethod:<span class="kw3">String</span> = <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#93;</span>, testMethod<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> testBasic<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span class="br0">&#40;</span><span class="st0">&quot;failing test&quot;</span>, <span class="nu0">1</span> + <span class="nu0">1</span> == <span class="nu0">5</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Notice that we have a basic test that will show up as a fail in ASUnit, so that we can be sure that we&#8217;ve hooked everything together successfully. As soon as you see the fail, you can delete it. Note also that the call to super() starts with an empty Array &#8211; in time we&#8217;ll populate with the classes that we wish to set up as mocks.</p>
<p>Now, let&#8217;s pause for a think about what we want to test here &#8211; the View relies on the proxy for its data, so we want to check that the view gets its data successfully from the proxy <em>without hitting the live Twitter servers</em>. Oh, and the proxy doesn&#8217;t exist yet, just an interface.</p>
<p>I&#8217;m going to start really slowly here, forgive me &#8211; adding the assert that checks the view&#8217;s title.</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">class</span> TwitterTest <span class="kw3">extends</span> ASUnitMockitoTestCase <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> TwitterTest<span class="br0">&#40;</span>testMethod:<span class="kw3">String</span> = <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#93;</span>, testMethod<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> testViewGetTitle<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;Twitter updates&quot;</span>, view.<span class="me1">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Immediate compile error, since we haven&#8217;t declared view, so let&#8217;s add another line: </p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> view:TwitterView = <span class="kw2">new</span> TwitterView<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">assertEquals<span class="br0">&#40;</span><span class="st0">&quot;Twitter updates&quot;</span>, view.<span class="me1">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Still no success, because the TwitterView cannot be instantiated without a ITwitterProxy, which we don&#8217;t have, so let&#8217;s mock that (note that I&#8217;ve also added ITwitterProxy to the super() in the TwitterTest() constructor).</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">class</span> TwitterTest <span class="kw3">extends</span> ASUnitMockitoTestCase <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> TwitterTest<span class="br0">&#40;</span>testMethod:<span class="kw3">String</span> = <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="br0">&#91;</span>ITwitterProxy<span class="br0">&#93;</span>, testMethod<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> testViewGetTitle<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> mockProxy:ITwitterProxy = mock<span class="br0">&#40;</span>ITwitterProxy<span class="br0">&#41;</span> as ITwitterProxy;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> view:TwitterView = <span class="kw2">new</span> TwitterView<span class="br0">&#40;</span>mockProxy<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.<span class="me1">init</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;Twitter updates&quot;</span>, view.<span class="me1">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Huzzah &#8211; the movie compiles and we get our failing test <img src='http://blog.flexiblediamond.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  For the record, mock() takes the interface and builds an object based on it that will record all calls made on its methods &#8211; it&#8217;s very, very clever.</p>
<p>First thing, let&#8217;s set the mockProxy up to give us the title string that we expect &#8211; add this line above the creation of the view:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1">given<span class="br0">&#40;</span>mockProxy.<span class="me1">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">willReturn</span><span class="br0">&#40;</span><span class="st0">&quot;Twitter updates&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>This beautiful line of code says: if someone calls getTitle() on the mockProxy, the mockProxy will return &#8220;Twitter updates&#8221;. Isn&#8217;t that cool? Of course, that doesn&#8217;t help pass the test just yet.</p>
<p>The view is expecting to receive a TwitterLoadEvent from the proxy, triggering onTwitterLoaded(), so our mock object needs to have IEventDispatcher functionality. However, because the mockObject does so much weird stuff behind the scenes that I don&#8217;t have (and don&#8217;t want to have) a clue about, I&#8217;m going to attack this another way.</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1">given<span class="br0">&#40;</span>mockProxy.<span class="me1">addEventListener</span><span class="br0">&#40;</span>any<span class="br0">&#40;</span><span class="br0">&#41;</span>, any<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">will</span><span class="br0">&#40;</span>fireImmediateLoadEvent<span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>When mockProxy.addEventListener() is called, it will fire an immediate load event &#8211; this is mocking the request/response communication with the Twitter server. So what&#8217;s fireImmediateLoadEvent?</p>
<p>It&#8217;s a GenericAnswer object, which just holds a function that will be called when addEventListener() is called; in this case I want it to be like this:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> fireImmediateLoadEvent:Answer = <span class="kw2">new</span> GenericAnswer<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// record the event and eventHandler somehow</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// then immediately fire the event with the required data</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Of course, in the real world, the proxy will be doing this as part of its work, but because we&#8217;re using a mock proxy, we have to work around it a bit. I&#8217;ve come up with this:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> d:EventDispatcher = <span class="kw2">new</span> EventDispatcher<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="br0">&#125;</span> as IEventDispatcher<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> fireImmediateLoadEvent:Answer = <span class="kw2">new</span> GenericAnswer<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// record the event and eventHandler somehow</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; d.<span class="me1">addEventListener</span><span class="br0">&#40;</span>TwitterLoadEvent.<span class="me1">TWITTER_LOADED</span>, view.<span class="me1">onTwitterLoaded</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// then immediately fire the event with the required data</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; d.<span class="me1">dispatchEvent</span><span class="br0">&#40;</span><span class="kw2">new</span> TwitterLoadEvent<span class="br0">&#40;</span>mockProxy<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>So now the test looks like this:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> testViewGetTitle<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// create an EventDispatcher that can be used as the dispatching functionality for</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// the mock ITwitterClient</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> d:EventDispatcher = <span class="kw2">new</span> EventDispatcher<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="br0">&#125;</span> as IEventDispatcher<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> mockProxy:ITwitterProxy = mock<span class="br0">&#40;</span>ITwitterProxy<span class="br0">&#41;</span> as ITwitterProxy;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; given<span class="br0">&#40;</span>mockProxy.<span class="me1">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">willReturn</span><span class="br0">&#40;</span><span class="st0">&quot;Twitter updates&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; given<span class="br0">&#40;</span>mockProxy.<span class="me1">getTweets</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">willReturn</span><span class="br0">&#40;</span><span class="br0">&#91;</span><span class="st0">&quot;Tweet#1&quot;</span>, <span class="st0">&quot;Tweet#2&quot;</span>, <span class="st0">&quot;Tweet#3&quot;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> view:TwitterView = <span class="kw2">new</span> TwitterView<span class="br0">&#40;</span>mockProxy<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> fireImmediateLoadEvent:Answer = <span class="kw2">new</span> GenericAnswer<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// add the listening class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.<span class="me1">addEventListener</span><span class="br0">&#40;</span>TwitterLoadEvent.<span class="me1">TWITTER_LOADED</span>, view.<span class="me1">onTwitterLoaded</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// then immediately fire the event &#8211; this mocks the XML-loading that really occurs</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d.<span class="me1">dispatchEvent</span><span class="br0">&#40;</span><span class="kw2">new</span> TwitterLoadEvent<span class="br0">&#40;</span>mockProxy<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; given<span class="br0">&#40;</span>mockProxy.<span class="me1">addEventListener</span><span class="br0">&#40;</span>any<span class="br0">&#40;</span><span class="br0">&#41;</span>, any<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">will</span><span class="br0">&#40;</span>fireImmediateLoadEvent<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; view.<span class="me1">init</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;Twitter updates&quot;</span>, view.<span class="me1">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>For completeness, here&#8217;s TwitterLoadEvent:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">class</span> TwitterLoadEvent <span class="kw3">extends</span> Event <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw3">static</span> const TWITTER_LOADED:<span class="kw3">String</span> = <span class="st0">&quot;onTwitterLoaded&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">private</span> <span class="kw2">var</span> _proxy:ITwitterProxy;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> TwitterLoadEvent<span class="br0">&#40;</span>proxy:ITwitterProxy<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span>TWITTER_LOADED, <span class="kw2">false</span>, <span class="kw2">false</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; _proxy = proxy;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">get</span> proxy<span class="br0">&#40;</span><span class="br0">&#41;</span>:ITwitterProxy <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> _proxy;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2009%2F08%2Fwhats-the-point-of-all-this-mockery%2F';
  addthis_title  = 'What%26%238217%3Bs+the+point+of+all+this+mockery%3F';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
<div id="wherego_related"><h3>Readers who viewed this page also viewed:</h3><ul><li><a href="http://blog.flexiblediamond.com/about/">About Us</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexiblediamond.com/2009/08/whats-the-point-of-all-this-mockery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rocks into Gold</title>
		<link>http://blog.flexiblediamond.com/2008/12/rocks-into-gold/</link>
		<comments>http://blog.flexiblediamond.com/2008/12/rocks-into-gold/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 23:25:23 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[management]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[pipeline]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=81</guid>
		<description><![CDATA[Clarke Ching &#8211; whose work I&#8217;ve been reading for a while, is preparing to publish a short parable for these troubled times. Get in touch with him via this post, and grab a copy, after all the more weapons in our armoury, the better chance we have of winning the inevitable battles.

  addthis_url  [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://clarkeching.blogs.com/">Clarke Ching</a> &#8211; whose work I&#8217;ve been reading for a while, is preparing to publish a short parable for these troubled times. <a href="http://www.clarkeching.com/2008/12/rocks-into-gold-a-credit-crunch-parable-for-people-who-build-software-for-a-living.html">Get in touch with him via this post</a>, and grab a copy, after all the more weapons in our armoury, the better chance we have of winning the inevitable battles.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F12%2Frocks-into-gold%2F';
  addthis_title  = 'Rocks+into+Gold';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexiblediamond.com/2008/12/rocks-into-gold/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Software development processes</title>
		<link>http://blog.flexiblediamond.com/2008/04/software-development-processes/</link>
		<comments>http://blog.flexiblediamond.com/2008/04/software-development-processes/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 14:35:47 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=31</guid>
		<description><![CDATA[One of the most concise and accurate summaries of available processes that I&#8217;ve come across. If only more devs and Project Managers printed this out, we would have a much more successful industry record.

  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Fsoftware-development-processes%2F';
  addthis_title  = 'Software+development+processes';
  addthis_pub    = '';

]]></description>
			<content:encoded><![CDATA[<p><a href="http://weblogs.java.net/blog/chet/archive/2008/01/crystal_methodo.html">One of the most concise and accurate summaries of available processes that I&#8217;ve come across</a>. If only more devs and Project Managers printed this out, we would have a much more successful industry record.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Fsoftware-development-processes%2F';
  addthis_title  = 'Software+development+processes';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexiblediamond.com/2008/04/software-development-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test-driving a HashMap</title>
		<link>http://blog.flexiblediamond.com/2008/04/test-driving-a-hashmap/</link>
		<comments>http://blog.flexiblediamond.com/2008/04/test-driving-a-hashmap/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 18:41:59 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=19</guid>
		<description><![CDATA[Right, here&#8217;s a TDD* walk-through, creating a new class tests-first.

HashMap
I&#8217;ve deliberately picked a fairly uninteresting class, as I want to focus on the process. It&#8217;s a utility data-holding object found in Java, more formalised than the ubiquitous Object that&#8217;s found in Flash applications the world over. That I&#8217;m stealing from Java is good, as we [...]]]></description>
			<content:encoded><![CDATA[<p>Right, here&#8217;s a TDD* walk-through, creating a new class tests-first.</p>
<p><span id="more-19"></span></p>
<p><strong>HashMap</strong><br />
I&#8217;ve deliberately picked a fairly uninteresting class, as I want to focus on the process. It&#8217;s a utility data-holding object found in Java, more formalised than the ubiquitous Object that&#8217;s found in Flash applications the world over. That I&#8217;m stealing from Java is good, as we don&#8217;t need to design it &#8211; we just need to implement the same API:</p>
<blockquote><p>
- size():Number<br />
- put(key:Object, value:Object):Object //returns previous value associated with key, or null<br />
- get(key:Object):Object //returns value mapped to key<br />
- clear():Void //empties the HashMap<br />
- containsKey(key:Object):Boolean<br />
- containsValue(value:Object):Boolean<br />
- isEmpty():Boolean<br />
- keySet():Array //returns an Array of keys<br />
- putAll(hashmap:HashMap):Void //puts the contents of another HashMap into this one<br />
- remove(key:Object):Object {//returns previous value associated with key, or null<br />
- values():Array //returns an Array of values
</p></blockquote>
<p>Handily that API gives us our to-do list for the exercise (though we won&#8217;t get through it all).</p>
<p><strong>Setting up the project</strong><br />
Firstly, if you don&#8217;t have asunit already, have a read-through this post which sets it up.</p>
<p>Set up a directory for this exercise. Create a new Actionscript 2.0 fla** in there called hashmap_tester.fla.</p>
<p>Add two lines to the first frame:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">import</span> AllTests;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> at:AllTests = <span class="kw2">new</span> AllTests<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Now test the movie.</p>
<p>It complains that it can&#8217;t find <code>AllTests</code>, so let&#8217;s create that:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> AllTests <span class="kw3">extends</span> com.<span class="me1">asunit</span>.<span class="me1">framework</span>.<span class="me1">TestSuite</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">private</span> <span class="kw2">var</span> className:<span class="kw3">String</span> = <span class="st0">&quot;AllTests&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> AllTests<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">//addTest(new HashMapTest());</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>and now testing throws no errors, and we&#8217;re good to think about the HashMap.</p>
<p><strong>Where to start?</strong><br />
Why are we even going down this route? Well, I don&#8217;t know about you, but I have a habit of thinking about a class for 5 minutes and then jumping straight into coding it. On a large/formal project, I plan enough to come up with a convincing overview of the likely class structure, but don&#8217;t drill down to a detailed level. Look, a class diagram, let&#8217;s get coding!</p>
<p>So, other folks&#8217; motivations may be different, but I follow a test-driven process because </p>
<ul>
<li>I need something to prevent me coding before I&#8217;ve thought about design, and</li>
<li>I&#8217;m sick of leaving all thought of testing to the end of a project when we&#8217;re already squeezed for time</li>
</ul>
<p>Therefore, we&#8217;re going to start with a single method from the Java API: <code>size()</code>, and define how we want it to behave (actually, <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html#size()">the Java boys have already done that</a>):</p>
<blockquote><p>size():int &#8211; Returns the number of key-value mappings in this map.</p></blockquote>
<p>Whilst that&#8217;s already clear, it&#8217;s not specific enough to write a test (humour me). So, let&#8217;s agree that calling size() on an empty HashMap will return 0, calling size() on a HashMap with a single entry will return 1, one with 2 entries will return 2, and so on. I know I&#8217;m stating the bleeding obvious, but these are the statements that will prove our job is done.</p>
<p><strong>Setting up the testing class</strong><br />
TDD uses unit tests, which verify single units of functionality, so we end up with a single class (HashMap) being tested by a single test class (HashMapTest).</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> HashMapTest <span class="kw3">extends</span> com.<span class="me1">asunit</span>.<span class="me1">framework</span>.<span class="me1">TestCase</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">private</span> <span class="kw2">var</span> className:<span class="kw3">String</span> = <span class="st0">&quot;HashMapTest&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> testIt<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertTrue<span class="br0">&#40;</span><span class="st0">&quot;failing test&quot;</span>, <span class="kw2">false</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now uncomment the line <code>addTest(new HashMapTest());</code> in AllTests.as, and test the movie. We get a red bar and the following message:</p>
<pre>
0 out of 1 asserts passed

----------------
Item Failed at :
assertion : assertTrue
message : failingtest
methodName : testIt
className : HashMapTest
</pre>
<p>That&#8217;s the information we need &#8211; which class the problem was in, which method, which assertion, etc. Well, alright, we could have worked all that out, but once there are lots of classes, these pointers will be invaluable.</p>
<p>The rules of TDD are really simple: <strong>Red, Green, Refactor</strong>.<br />
We&#8217;ve got red, which when considering we started with nothing is already a major achievement, so we need to take the next step. Change <code>false</code> to <code>true</code> in the <code>assertTrue</code> statement, and run the movie again. Whatcha got?</p>
<p>Green is cool, calming, and the place to be. In TDD, we start with red, get to green <em>as quickly as possible</em>, and stay there.</p>
<p>Since there isn&#8217;t much refactoring to be done with this test, let&#8217;s move on to writing a test for <code>size()</code>, starting with an empty HashMap.</p>
<p><strong>Testing size()</strong></p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> testSize<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> instance:HashMap = <span class="kw2">new</span> HashMap<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;empty HashMap.size() == 0&quot;</span>, <span class="nu0">0</span>, instance.<span class="kw3">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Run it, and you get a compile error, which handily points out our next move: let&#8217;s create the HashMap class and give it a <code>size()</code> method returning a <code>Number</code>.</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> HashMap <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> HashMap<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Number</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Testing shows we&#8217;re at red, which is progress.<br />
Now all we need to do is get to green, in the simplest and quickest (and dirtiest) way possible. So, I&#8217;m changing <code>return null</code> to <code>return 0</code>.</p>
<p>Obviously, I&#8217;m tricking the test, because we should be testing that there are no entries in there, but that comes with refactoring. For now we&#8217;re just aiming to pass the test, and we do the simplest thing to achieve that.</p>
<p>What&#8217;s next: calling <code>size</code> on a HashMap with a single entry returns <code>1</code>.<br />
But, before we write that test, we need to think about how to put an entry into the HashMap</p>
<p><strong>Putting something into the HashMap</strong></p>
<blockquote><p>
put(key:Object, value:Object):Object //returns previous value associated with key, or null
</p></blockquote>
<p>So, again starting with the tests: the behaviour we&#8217;re specifying is:</p>
<ul>
<li>putting an element (with key &#8220;element1&#8243; and value &#8220;value1&#8243;) into an empty HashMap returns null</li>
<li>putting an element (with key &#8220;element1&#8243; and value &#8220;value2&#8243;) into the HashMap returns the first element (key:&#8221;element1&#8243;, value:&#8221;value1&#8243;)</li>
<li>putting an element (with key &#8220;element2&#8243; and value &#8220;value2&#8243;) into the HashMap returns null</li>
</ul>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> testPut<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> instance:HashMap = <span class="kw2">new</span> HashMap<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> elementKey:<span class="kw3">String</span> = <span class="st0">&quot;element1&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> elementValue:<span class="kw3">String</span> = <span class="st0">&quot;value1&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertNull<span class="br0">&#40;</span><span class="st0">&quot;putting element into empty HashMap returns null&quot;</span>, instance.<span class="me1">put</span><span class="br0">&#40;</span>elementKey, elementValue<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Since we know the compiler&#8217;s going to baulk, let&#8217;s avoid that by adding the following to HashMap.as:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> put<span class="br0">&#40;</span><span class="kw3">key</span>:<span class="kw3">String</span>, value:<span class="kw3">Object</span><span class="br0">&#41;</span>:<span class="kw3">Object</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="kw3">key</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Note that I&#8217;m returning <code>key</code> to force the test to fail. And fail it does, so change it to <code>return null;</code> and we&#8217;re green. Time to add another assertion. Just duplicate the first, so the same key is being added to the HashMap again, but change the assert itself to <code>assertNotNull</code>.</p>
<p>Dang, fix one thing, break another. Well, this is where the refactoring part of the triad comes in: we need to refactor the code so that it passes both tests. (&#8221;Refactor&#8221; is much better than &#8220;write&#8221;, don&#8217;t you agree? It means that the code is already written, but just needs changing. And that&#8217;s the point &#8211; it is already written in my head).</p>
<p>So what to do? Well the HashMap is essentially a wrapper around a Plain Old Actionscript Object, isn&#8217;t it? If we&#8217;re going to return the first element when we add the second, we need to keep those elements somewhere, so let&#8217;s create a private object to hold them in the HashMap.</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> HashMap <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">private</span> <span class="kw2">var</span> map:<span class="kw3">Object</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> HashMap<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; map = <span class="kw2">new</span> <span class="kw3">Object</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Number</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> put<span class="br0">&#40;</span><span class="kw3">key</span>:<span class="kw3">String</span>, value:<span class="kw3">Object</span><span class="br0">&#41;</span>:<span class="kw3">Object</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Right, if a key doesn&#8217;t already exist in the HashMap, return null, but if it does return its previous value. How should that look?</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> put<span class="br0">&#40;</span><span class="kw3">key</span>:<span class="kw3">String</span>, value:<span class="kw3">Object</span><span class="br0">&#41;</span>:<span class="kw3">Object</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> returnObj:<span class="kw3">Object</span> = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>map<span class="br0">&#91;</span><span class="kw3">key</span><span class="br0">&#93;</span> <span class="sy0">!</span>= <span class="kw3">undefined</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; returnObj = map<span class="br0">&#91;</span><span class="kw3">key</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; map<span class="br0">&#91;</span><span class="kw3">key</span><span class="br0">&#93;</span> = value;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> returnObj;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Well, give it a go, since I&#8217;m coding without thinking too much, and let the tests decide. And they say &#8216;PASS&#8217; &#8211; all is green. Fantastic. Let&#8217;s add the final part of our tests for <code>put</code> to HashMapTest:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> testPut<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> instance:HashMap = <span class="kw2">new</span> HashMap<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> elementKey:<span class="kw3">String</span> = <span class="st0">&quot;element1&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">var</span> elementValue:<span class="kw3">String</span> = <span class="st0">&quot;value1&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertNull<span class="br0">&#40;</span><span class="st0">&quot;putting element into empty HashMap returns null&quot;</span>, instance.<span class="me1">put</span><span class="br0">&#40;</span>elementKey, elementValue<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertNotNull<span class="br0">&#40;</span><span class="st0">&quot;putting element into empty HashMap returns null&quot;</span>, instance.<span class="me1">put</span><span class="br0">&#40;</span>elementKey, elementValue<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertNull<span class="br0">&#40;</span><span class="st0">&quot;putting element into empty HashMap returns null&quot;</span>, instance.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;element2&quot;</span>, <span class="st0">&quot;value2&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>And it still works! That&#8217;s cool.</p>
<p><strong>Size() re-visited</strong><br />
So now we are able to put elements into the HashMap and have them stay there, we can end the trickery that&#8217;s getting us past the <code>size</code> tests. </p>
<p>Add another assert to <code>testSize</code> in HashMapTest.as:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> testSize<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Void</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> instance:HashMap = <span class="kw2">new</span> HashMap<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;empty HashMap.size() == 0&quot;</span>, <span class="nu0">0</span>, instance.<span class="kw3">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; instance.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;key&quot;</span>, <span class="st0">&quot;value&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;HashMap with 1 element, size() == 1&quot;</span>, <span class="nu0">1</span>, instance.<span class="kw3">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>That&#8217;s the test. I&#8217;m happy. It compiles and the asunit test runner shows red. I&#8217;m happier. </p>
<p>What is <code>size()</code> supposed to give us? Information about how many elements are in the HashMap, i.e. how many elements are in the private variable <code>map</code>. So we can just iterate through that, and keep a count of how many properties it has:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">public</span> <span class="kw2">function</span> <span class="kw3">size</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Number</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">var</span> count:<span class="kw3">Number</span> = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">for</span><span class="br0">&#40;</span><span class="kw2">var</span> p <span class="kw1">in</span> map<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; count++;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">return</span> count;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>And I reckon that should pass.</p>
<p><strong>To-do list</strong><br />
I&#8217;ve missed a good test out here &#8211; now we know <code>size()</code> is working, then we can use it to verify <code>put</code>&#8217;s behaviour. Specifically, if we add elements using different keys, <code>size</code> will increase each time; but if we add elements using the same key, then they will replace each other and the <code>size</code> will remain the same.</p>
<p>It&#8217;s handy to have a list &#8211; paper- or computer-based &#8211; nearby to record such thoughts when they appear, since you&#8217;re likely to be in the middle of writing a test or refactoring code to pass a test. It also allows you to prioritise, and to go home knowing you can return in the morning and know where to pick up.</p>
<p>Which reminds me. Always finish the day with a failing test. The next morning you turn up, run the tests, and know exactly what you were going to do next. That&#8217;s easy, isn&#8217;t it? (N.B. If you&#8217;re working in a team with source code control &#8211; you are, aren&#8217;t you? &#8211; do not check in this failing test, that might upset the others).</p>
<p><strong>Homework</strong><br />
Well, when did someone last give you homework to do?<br />
You write out the tests to cover the rest of the Java HashMap API. And, if you really fancy it, code the implementation too.</p>
<p><strong>The TDD rhythym</strong><br />
1. Quickly add a test.<br />
2. Run all tests and see the new one fail.<br />
3. Make a little change.<br />
4. Run all tests and see them all succeed.<br />
5. Refactor to remove duplication.</p>
<p>Red, green, refactor.<br />
Red, green, refactor.<br />
Red, green, refactor.</p>
<p>* Test-Driven Development<br />
** I&#8217;m currently working on a project porting AS1.0 to AS2.0, nary a sign of AS3.0. I believe that there are other AS2-ers out there who need information about unit-testing and test-driven development, so I&#8217;m fairly unapologetic about using AS2.0. If you are only coding AS3.0 (lucky you!) and can&#8217;t find resources, shout and I&#8217;ll have a stab at re-writing this for AS3.0, using flexunit.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Ftest-driving-a-hashmap%2F';
  addthis_title  = 'Test-driving+a+HashMap';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexiblediamond.com/2008/04/test-driving-a-hashmap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Urgency is poisonous&#8221;</title>
		<link>http://blog.flexiblediamond.com/2008/04/urgency-is-poisonous/</link>
		<comments>http://blog.flexiblediamond.com/2008/04/urgency-is-poisonous/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 08:02:43 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[process]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[business]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=21</guid>
		<description><![CDATA[37 Signals are experimenting with a 4-day week, and finding it works.
They reckon that &#8220;urgency is acidic&#8221; &#8211; it burns out morale, especially when working towards an emergency deadline lasts more than a day or two (like, say 7 weeks plus &#8211; you know who you are!).

If your deliveries are that critical to the hour [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.37signals.com/svn/posts/966-urgency-is-poisonous">37 Signals</a> are experimenting with a 4-day week, and finding it works.</p>
<p>They reckon that &#8220;urgency is acidic&#8221; &#8211; it burns out morale, especially when working towards an emergency deadline lasts more than a day or two (like, say 7 weeks plus &#8211; you know who you are!).</p>
<blockquote><p>
If your deliveries are that critical to the hour or day, maybe you&#8217;re setting up false priorities and dangerous expectations.
</p></blockquote>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Furgency-is-poisonous%2F';
  addthis_title  = '%26%238220%3BUrgency+is+poisonous%26%238221%3B';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexiblediamond.com/2008/04/urgency-is-poisonous/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Perishable Requirements&#8221;</title>
		<link>http://blog.flexiblediamond.com/2008/04/perishable-requirements/</link>
		<comments>http://blog.flexiblediamond.com/2008/04/perishable-requirements/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 12:30:53 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[process]]></category>
		<category><![CDATA[agile]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=18</guid>
		<description><![CDATA[Re-reading Agile Management for Software Engineering*, I got stuck on the phrase &#8220;perishable requirements&#8221;.
I re-read it several times, and each time it sunk in deeper just how important this concept is: this alone proves the worth of an agile approach to project development. Requirements go stale.
Most obviously, they run the risk of becoming obselete as [...]]]></description>
			<content:encoded><![CDATA[<p>Re-reading <em><a href="http://www.amazon.co.uk/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.co.uk%2FAgile-Management-Software-Engineering-Constraints%2Fdp%2F0131424602%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1208780691%26sr%3D8-1&#038;tag=hoarhope-21&#038;linkCode=ur2&#038;camp=1634&#038;creative=6738">Agile Management for Software Engineering</a></em><img src="http://www.assoc-amazon.co.uk/e/ir?t=hoarhope-21&amp;l=ur2&amp;o=2" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />*, I got stuck on the phrase &#8220;perishable requirements&#8221;.</p>
<p>I re-read it several times, and each time it sunk in deeper just how important this concept is: this alone proves the worth of an agile approach to project development. <em>Requirements go stale</em>.</p>
<p>Most obviously, they run the risk of becoming obselete as the team and client&#8217;s understanding of the project evolves; but, equally importantly, they suffer atrophy simply through lack of use.</p>
<p>Once captured and defined, a requirement has a half-life &#8211; the longer it gets left in the requirements document without being actively worked on, the poorer the knowledge of that requirement. Clearly, if the developer leaves, the team suffers massive impact in its understanding of the requirement. But, even if the developer stays, their understanding and knowledge of the requirement withers as they work on other things.</p>
<p>The reality is that a requirement can rarely be so fully captured that it doesn&#8217;t depend on an individual&#8217;s understanding and definition of it. </p>
<p>The solution to this problem?</p>
<p>Define a sub-set of requirements that can be worked on within a specific timeframe. Move from requirements capture and definition to solution implementation within the same timeframe. Work agilely.</p>
<p><a href="http://www.amazon.co.uk/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.co.uk%2FAgile-Management-Software-Engineering-Constraints%2Fdp%2F0131424602%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1208780691%26sr%3D8-1&#038;tag=hoarhope-21&#038;linkCode=ur2&#038;camp=1634&#038;creative=6738">* Eli Schragenheim &#038; David J. Anderson, Prentice Hall, 2003 (ISBN: 0-13-142460-2)</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=hoarhope-21&amp;l=ur2&amp;o=2" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Fperishable-requirements%2F';
  addthis_title  = '%26%238220%3BPerishable+Requirements%26%238221%3B';
  addthis_pub    = '';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://blog.flexiblediamond.com/2008/04/perishable-requirements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
