<?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; puremvc</title>
	<atom:link href="http://blog.flexiblediamond.com/tag/puremvc/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>Testing Proxies in PureMVC</title>
		<link>http://blog.flexiblediamond.com/2008/06/testing-proxies-in-puremvc/</link>
		<comments>http://blog.flexiblediamond.com/2008/06/testing-proxies-in-puremvc/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 13:13:25 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[puremvc]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=55</guid>
		<description><![CDATA[This post is prompted by Larry Marburger&#8217;s article, since I came across this problem a few weeks ago, and found a different solution.
The scenario
There&#8217;s this great new framework that you&#8217;re starting to use, and it&#8217;s persuaded you to turn a new leaf and unit-test your work as you go. First off, be warned this is [...]]]></description>
			<content:encoded><![CDATA[<p>This post is prompted by <a href="http://developmentastic.com/2008/06/09/flexunit-testing-puremvc-code/">Larry Marburger&#8217;s article</a>, since I came across this problem a few weeks ago, and found a different solution.</p>
<p><strong>The scenario</strong><br />
There&#8217;s <a href="http://www.puremvc.org">this great new framework</a> that you&#8217;re starting to use, and it&#8217;s persuaded you to turn a new leaf and unit-test your work as you go. First off, be warned this is all AS2.0, using <a href="http://www.asunit.org">asunit2.5</a>, because I&#8217;m still stuck in the dark ages <img src='http://blog.flexiblediamond.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p><strong>The problem</strong><br />
PureMVC uses Proxy objects to access data from the model, which fire off Notifications (PureMVC-specific events) when the data is ready. However, in the case of a Proxy that loads XML before making its data available, how do we know when it&#8217;s ready to be tested?</p>
<p><span id="more-55"></span><br />
<strong>Solution </strong><br />
Mediator objects sign up to receive notification of Proxy events in PureMVC, so we can subclass the Mediator to create a class that can run tests on the Proxy.</p>
<p><strong>Example please</strong><br />
ASUnit calls <code>run()</code> for every TestCase in the TestSuite, the default implementation of which calls <code>runNow()</code> to run the tests within a class. So I&#8217;ve overridden <code>run()</code> to create the Proxy we want to test. When the Proxy is ready, it will notify the TestCase, and <code>runNow()</code> will be called.</p>
<p>In order to achieve this, I&#8217;ve created a TestHelper, which subclasses Mediator, to capture the Proxy&#8217;s completion Notification. The TestHelper tells the TestCase that it&#8217;s good to go, and <code>runNow()</code> gets called.</p>
<p><strong>A thousand words</strong></p>
<p><a href='http://blog.hoardinghopes.com/wp-content/uploads/2008/06/sequence.png'><img src="http://blog.hoardinghopes.com/wp-content/uploads/2008/06/sequence-300x214.png" alt="" title="Sequence for testing Proxies in PureMVC" width="300" height="214" class="alignnone size-medium wp-image-56" /></a></p>
<p><strong>Implementation Specifics</strong></p>
<p>First, the class we&#8217;re interested in, and as you can see, it&#8217;s very simple. It registers with the PureMVC ApplicationFacade for <code>Events.XML_LOADED</code> Notifications, and calls the responseHandler it was given by when created by NavigationProxyTest (<code>runNow()</code>).</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">import</span> org.<span class="me1">puremvc</span>.<span class="me1">as2</span>.<span class="me1">interfaces</span>.<span class="me1">IMediator</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">import</span> org.<span class="me1">puremvc</span>.<span class="me1">as2</span>.<span class="me1">interfaces</span>.<span class="me1">INotification</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">import</span> org.<span class="me1">puremvc</span>.<span class="me1">as2</span>.<span class="me1">patterns</span>.<span class="me1">mediator</span>.<span class="me1">Mediator</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">import</span> Events;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> NavigationProxyTestHelper <span class="kw3">extends</span> Mediator <span class="kw3">implements</span> IMediator <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="kw3">static</span> <span class="kw2">var</span> <span class="kw3">NAME</span>:<span class="kw3">String</span> = <span class="st0">&quot;NavigationProxyTestHelper&quot;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw3">private</span> <span class="kw2">var</span> _responseHandler:<span class="kw2">Function</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> NavigationProxyTestHelper<span class="br0">&#40;</span>view:<span class="kw3">Object</span>, <span class="kw3">name</span>:<span class="kw3">String</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">super</span><span class="br0">&#40;</span><span class="kw3">name</span> <span class="sy0">||</span> <span class="kw3">NAME</span>, view<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> listNotificationInterests<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw3">Array</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">return</span> <span class="br0">&#91;</span>Events.<span class="me1">XML_LOADED</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;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw3">public</span> <span class="kw2">function</span> handleNotification<span class="br0">&#40;</span>notification:INotification<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="kw1">switch</span><span class="br0">&#40;</span>notification.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">case</span> Events.<span class="me1">XML_LOADED</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; _responseHandler<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</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> setResponseHandler<span class="br0">&#40;</span>response:<span class="kw2">Function</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; _responseHandler = response;</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>The NavigationProxyTest TestCase sets up the TestHelper in <code>run()</code>, and registers it with the ApplicationFacade. Then it creates the NavigationProxy instance that we want to test. When the Proxy is fully-loaded and raring to go, <code>TestCase#runNow()</code> will be called, which calls all methods starting &#8220;test&#8221; &#8211; so in this example <code>testValuesFromXML()</code>.</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> NavigationProxyTest <span class="kw3">extends</span> TestCase <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> instance:NavigationProxy;</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> run<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> mediator:NavigationProxyTestHelper = <span class="kw2">new</span> NavigationProxyTestHelper<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw3">createEmptyMovieClip</span><span class="br0">&#40;</span><span class="st0">&quot;empty_mc&quot;</span>, <span class="nu0">1</span><span class="br0">&#41;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; NavigationProxyTestHelper.<span class="kw3">NAME</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; ApplicationFacade.<span class="me1">getInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">registerMediator</span><span class="br0">&#40;</span>mediator<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; NavigationProxyTestHelper<span class="br0">&#40;</span>mediator<span class="br0">&#41;</span>.<span class="me1">setResponseHandler</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Delegate.<span class="me1">create</span><span class="br0">&#40;</span><span class="kw3">this</span>, runNow<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; instance = <span class="kw2">new</span> NavigationProxy<span class="br0">&#40;</span><span class="st0">&quot;navigation.xml&quot;</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> testValuesFromXML<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;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co1">// initialDestination &amp; showIntroVideo are found in the XML that the NavigationProxy loads</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;destinationID = 72&quot;</span>, <span class="nu0">72</span>, instance.<span class="me1">initialDestination</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertFalse<span class="br0">&#40;</span><span class="st0">&quot;showIntroVideo = true&quot;</span>, instance.<span class="me1">showIntroVideo</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"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F06%2Ftesting-proxies-in-puremvc%2F';
  addthis_title  = 'Testing+Proxies+in+PureMVC';
  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/2009/07/mocking-slavery/">Mocking slavery</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexiblediamond.com/2008/06/testing-proxies-in-puremvc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PureMVC AS2.0 Unit Tests released</title>
		<link>http://blog.flexiblediamond.com/2008/04/puremvc-as20-unit-tests-released/</link>
		<comments>http://blog.flexiblediamond.com/2008/04/puremvc-as20-unit-tests-released/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 11:03:09 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[puremvc]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=27</guid>
		<description><![CDATA[I&#8217;m really pleased to announce that the port of the PureMVC unit tests from AS3 to AS2 is complete, barring an update of the wiki.
You can get the details here, or export/checkout directly from the Subversion repository here.
This has pretty much been a simple port of the AS3 tests, but I&#8217;ve modified some of the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m really pleased to announce that the port of the PureMVC unit tests from AS3 to AS2 is complete, barring an update of the wiki.</p>
<p>You can <a href="http://trac.puremvc.org/PureMVC_AS2_UnitTests">get the details here</a>, or <a href="http://svn.puremvc.org/PureMVC_AS2_UnitTests/">export/checkout directly from the Subversion repository here</a>.</p>
<p>This has pretty much been a simple port of the AS3 tests, but I&#8217;ve modified some of the <a href="http://en.wikipedia.org/wiki/Mock_object">mock objects</a> required for the tests. Hopefully it all makes sense.</p>
<p>The AS2.0 tests make use of <a href="http://www.asunit.org">the AsUnit framework</a>.</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Fpuremvc-as20-unit-tests-released%2F';
  addthis_title  = 'PureMVC+AS2.0+Unit+Tests+released';
  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/puremvc-as20-unit-tests-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit testing Actionscript 2.0 with ASUnit</title>
		<link>http://blog.flexiblediamond.com/2008/04/unit-testing-actionscript-20-with-asunit/</link>
		<comments>http://blog.flexiblediamond.com/2008/04/unit-testing-actionscript-20-with-asunit/#comments</comments>
		<pubDate>Wed, 23 Apr 2008 17:11:00 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tilly]]></category>
		<category><![CDATA[puremvc]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=14</guid>
		<description><![CDATA[We&#8217;re going to have a shot at unit-testing actionscript 2.0, after we&#8217;ve decided what unit-testing is, and why&#8217;s it&#8217;s worth doing. 

Firstly, what&#8217;s going on? Aren&#8217;t I a bit behind the times? AS2.0, indeed. Well, yes, you&#8217;re absolutely right &#8211; but right now that&#8217;s where my money&#8217;s currently coming from: bringing a timeline-based AS1.0 project [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re going to have a shot at unit-testing actionscript 2.0, after we&#8217;ve decided what unit-testing is, and why&#8217;s it&#8217;s worth doing. </p>
<p><span id="more-14"></span></p>
<p>Firstly, what&#8217;s going on? Aren&#8217;t I a bit behind the times? AS2.0, indeed. Well, yes, you&#8217;re absolutely right &#8211; but right now that&#8217;s where my money&#8217;s currently coming from: bringing a timeline-based AS1.0 project slightly closer to the present (AS3.0 isn&#8217;t an option, as we&#8217;re still targetting the Flash 8 player).</p>
<p><strong>What is unit testing?</strong><br />
<a href="http://en.wikipedia.org/wiki/Unit_testing">Wikipedia says</a>:</p>
<blockquote><p>
&#8230; unit testing is a procedure used to validate that individual units of source code are working properly. A unit is the smallest testable part of an application. In [...] object-oriented programming, the smallest unit is a method.
</p></blockquote>
<p>So, we&#8217;re breaking our application down into the smallest possible pieces &#8211; methods &#8211; and testing them to ensure that, at that basic level, they do what we expect (and that they don&#8217;t do what we don&#8217;t expect them to).</p>
<p>Most languages that I&#8217;ve heard of have a unit-testing framework, which automates the process of running through each of the tests. Such a framework ensures:</p>
<ul>
<li>testing is easy</li>
<li>testing is fast</li>
<li>testing is fun (this isn&#8217;t pure/sad geek joy, there&#8217;s visceral satisfasfaction to be had in turning red to green. More anon.)</li>
<li>each test is run in isolation, with state being wiped clean before the next test is run</li>
<li>a body of tests is built up and run each time, which ensures that making a change here doesn&#8217;t break something over there, or that re-writing this bit today doesn&#8217;t break all that good work.</li>
</ul>
<p>All of which is pretty good, isn&#8217;t it? Another advantage for developers like me &#8211; who do a certain amount of design, but then as soon as we start coding struggle to step back to do more design &#8211; is that writing tests requires us to think about how our code is going to work, in what situations we want it to fail and how. That&#8217;s because I practise* <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test-Driven Development</a> (thanks, <a href="http://en.wikipedia.org/wiki/Kent_Beck">Kent Beck</a>), which I&#8217;ll talk more about later, if time is kind.</p>
<p><strong>AsUnit</strong></p>
<p>As I said most languages have their framework (JUnit for Java, NUnit for .NET, etc., collectively known as xUnit), and thankfully Actionscript 2 is no exception. <a href="http://www.lukebayes.com">Luke Bayes</a> and <a href="http://www.alimills.com">Ali Mills</a> created ASUnit, which provides a test-running GUI, and 3 (three, like one&#8217;s not enough!) frameworks. </p>
<p><a href='http://blog.hoardinghopes.com/wp-content/uploads/2008/04/asunit.png'><img src="http://blog.hoardinghopes.com/wp-content/uploads/2008/04/asunit.png" alt="The ASUnit test GUI" title="asunit" width="416" height="430" class="aligncenter size-full wp-image-25" style="display:block; clear:both; margin-right:10px" /></a><br />
<br style="display:block; clear:both;" /></p>
<p>So, what does it do? It&#8217;s just a blank text area and a green bar. Yep, pretty much, and that&#8217;s as simple as you want it &#8211; not much there. Run your hundreds of passing tests, and you&#8217;ll see the same thing &#8211; that&#8217;s the point: when you&#8217;re getting stuff right, it keeps out of the way.</p>
<p><a href='http://blog.hoardinghopes.com/wp-content/uploads/2008/04/red_asunit.png'><img src="http://blog.hoardinghopes.com/wp-content/uploads/2008/04/red_asunit.png" alt="The ASUnit test GUI with failing tests" title="red_asunit" width="416" height="430" class="aligncenter size-full wp-image-24" style="display:block; clear:both; margin-right:10px" /></a><br />
<br style="display:block; clear:both;" /></p>
<p>When something goes wrong, however, the bar goes red. Just one ickle lickle slip, and there it is. Blood-soaked redness. When your eyes are criss-crossed with tendrils of that self same colour in the early hours, the bar will let you know. And it&#8217;ll point out which tests failed: class name, method name, assertion type and message, which does a great job of narrowing down the problem (that reminds me, we once had a brilliant site, which I loved and was very proud of, but &#8211; this is back in the dark days of Flash 5 &#8211; bugs would appear and be extremely expensive, because even though they only took 15 minutes to fix, they took 2 days to find).</p>
<p>So there you pretty much have it: write code, the bar&#8217;s green, continue. Write code, the bar&#8217;s red, find and fix the problem.</p>
<p></p>
<p><strong>A couple of tests to prove the point</strong><br />
If you&#8217;ve installed the MXP, it&#8217;s given you a couple of commands in the IDE to make your job even easier (thanks, guys).<br />
- Create a new AS2.0 fla, and save it somewhere &#8211; that&#8217;s important because a whole lot of files are about to be created recursively, so start from a dank dead-end corner of your hard drive, and those files won&#8217;t infect everything (i.e. NOT your desktop).<br />
- command > create class, give it the name Example, set it to open automatically in your AS editor, don&#8217;t bother with adding a symbol. Hit it.<br />
- open the asunit gui<br />
- in the first frame of the fla:</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>- Tadaaa!: your first failing test. That&#8217;s a bit poor, isn&#8217;t it?</p>
<p>Well no, not really. ASUnit has given you a failing test to prove that the command worked, and to give you somewhere to start. There&#8217;s nothing quite like making that first move. Notice how the bar&#8217;s red. That right there is something to work on.</p>
<p>Go to the failing test (ExampleTest.as), which should be open in your editor, and take a look &#8211; the offending line is</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1">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>
</ol>
</div>
<p>Easily rectified: change <code>false</code> to <code>true</code>, save, test the movie again. The bar is now green. Did you feel it? Did ya, did ya? That nice fluttery feeling in your belly, the slightest drop of tension in your shoulders? That&#8217;s going to become a Pavlovian reaction to seeing red turn to green, and makes waiting at traffic lights much more bearable.</p>
<p>So, another quick example, open your test class and add the following:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> testIsHappy<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> mood:<span class="kw3">String</span> = <span class="st0">&quot;sad&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; instance.<span class="me1">setMood</span><span class="br0">&#40;</span>mood<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;This example makes me happy&quot;</span>, <span class="st0">&quot;sad&quot;</span>, instance.<span class="me1">getMood</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>test the movie &#8211; compile error!<br />
That&#8217;s a good thing &#8211; I&#8217;m working by defining the way I want the functionality to behave before I write it. That means I&#8217;m thinking about the design before I write it. That by itself has to give you a warm, fuzzy feeling, doesn&#8217;t it?</p>
<p>onwards.</p>
<p>open your class and add the following:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">private</span> <span class="kw2">var</span> _mood:<span class="kw3">String</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> setMood<span class="br0">&#40;</span>mood:<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; _mood = mood;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> getMood<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; <span class="kw1">return</span> _mood;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>test the movie &#8211; red bar!</p>
<p>Change the test to:</p>
<div class="geshi no actionscript">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">public</span> <span class="kw2">function</span> testIsHappy<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> mood:<span class="kw3">String</span> = <span class="st0">&quot;happy&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; instance.<span class="me1">setMood</span><span class="br0">&#40;</span>mood<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; assertEquals<span class="br0">&#40;</span><span class="st0">&quot;This example makes me happy&quot;</span>, <span class="st0">&quot;happy&quot;</span>, instance.<span class="me1">getMood</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>and truly, you will be.</p>
<p><strong>Available assertions</strong></p>
<ul>
<li>assertTrue</li>
<li>assertFalse</li>
<li>assertNull</li>
<li>assertNotNull</li>
<li>assertUndefined</li>
<li>assertNotUndefined</li>
<li>assertEquals</li>
<li>assertSame</li>
</ul>
<p>Each of which is fairly self-explanatory &#8211; the expected parameters for the last two are (message, expectedResult, actualResult), and for the others: (message, <object being tested>).</p>
<p>* why does the verb &#8216;practise&#8217; have religious connotations, or is that just me? As it happens, with TDD the subject, it&#8217;s not far wrong, but that&#8217;s another story &#8230;</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Funit-testing-actionscript-20-with-asunit%2F';
  addthis_title  = 'Unit+testing+Actionscript+2.0+with+ASUnit';
  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/2008/04/unit-testing-actionscript-20-with-asunit/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unit-testing PureMVC</title>
		<link>http://blog.flexiblediamond.com/2008/04/unit-testing-puremvc/</link>
		<comments>http://blog.flexiblediamond.com/2008/04/unit-testing-puremvc/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 06:22:41 +0000</pubDate>
		<dc:creator>jamesk</dc:creator>
				<category><![CDATA[Stuff]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[puremvc]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.hoardinghopes.com/?p=13</guid>
		<description><![CDATA[My current project is a timeline-based Actionscript 1.0 app, which is about as unmaintainable as it gets. When I started the backlog of change requests and updates was at least three months long, so making the site more easily updatable was a priority, in among doing the updates.
It&#8217;s a sprawling mass of code, with no [...]]]></description>
			<content:encoded><![CDATA[<p>My current project is a timeline-based Actionscript 1.0 app, which is about as unmaintainable as it gets. When I started the backlog of change requests and updates was at least three months long, so making the site more easily updatable was a priority, in among doing the updates.</p>
<p>It&#8217;s a sprawling mass of code, with no division of responsibility anywhere, so bits of code spread out over the whole system are wired into each other, and it all feels like I&#8217;m left with wire-cutters and a bomb.</p>
<p>Enter <a href="http://www.puremvc.org">PureMVC</a>.</p>
<p><span id="more-13"></span></p>
<p>I&#8217;ve been looking for a panacea-like framework for some time, and this may just be it. The documentation is excellent. Can I say that again, and emphasise how important it is? <strong>The documentation is excellent.</strong></p>
<p>And, more recently, it&#8217;s been ported backwards to Actionscript 2.0, which is my current domain, so I can use it to pull this labyrinthine project into shape. </p>
<p>I noted, however, that the unit tests hadn&#8217;t been ported back, so I converted them on a commute. Why? Apart from anything else, the tests demonstrate how the original architect and developer, Cliff Hall, wanted his framework to be used. </p>
<p>So, converting a legacy project without breaking any of it: unit tests are pretty much my only safeguard. As I go through the old code and find an exception to a rule (and there are lots), I can write a test to verify that behaviour, and then get on with re-writing the code. As long as the tests continue to pass, I&#8217;m safe (until next time!).</p>
<p>Two follow-ups required: how to write unit tests for Actionscript 2.0 (hint: take a look at <a href="http://asunit.org">asunit</a>); how to write tests around the peculiarities of puremvc? More to follow &#8230;</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fblog.flexiblediamond.com%2F2008%2F04%2Funit-testing-puremvc%2F';
  addthis_title  = 'Unit-testing+PureMVC';
  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/unit-testing-puremvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
