<?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>Saturnboy &#187; flexunit4</title>
	<atom:link href="http://saturnboy.com/tag/flexunit4/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Thu, 19 Aug 2010 03:28:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Async Testing with FlexUnit 4</title>
		<link>http://saturnboy.com/2010/02/async-testing-with-flexunit4/</link>
		<comments>http://saturnboy.com/2010/02/async-testing-with-flexunit4/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 20:58:53 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[flexunit4]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1019</guid>
		<description><![CDATA[I tend to spend a large portion of my development time worrying about the various interfaces across the application. I like to worry about UX (aka the interface between the user and my app). I like to worry about the ORM (aka the interface between the database and my code). And I especially like to [...]]]></description>
			<content:encoded><![CDATA[<p>I tend to spend a large portion of my development time worrying about the various interfaces across the application.  I like to worry about UX (aka the interface between the user and my app).  I like to worry about the ORM (aka the interface between the database and my code).  And I especially like to worry about the client-side service layer (aka the interface between the backend and the frontend).  When I worry, I very quickly find myself writing tests.</p>
<p>The new hotness in Flex testing is, of course, <a href="http://www.gorillalogic.com/flexmonkey">FlexMonkey</a>, developed and open-sourced by my company, <a href="http://www.gorillalogic.com/">Gorilla Logic</a>.  The next best Flex testing platform, and the new hotness in its own right, is <a href="http://www.flexunit.org/">FlexUnit 4</a>, developed and open-sourced by our partners at <a href="http://www.digitalprimates.com/">Digital Primates</a>.  FlexUnit 4 is <i>the</i> Flex 4 unit testing framework.  Because of its awesome async testing support, along with many other great features, it is ideally suited to test client-side service layers.  In this post, I&#8217;m going to explore async testing with FlexUnit 4 to better understand how I can help mitigate the pain of asynchronous backend services that are ever-present in enterprise Flex applications.</p>
<h3>Test: Create Team</h3>
<p>Let&#8217;s imagine I have my favorite data model of teams and players, with a one-to-many relationship between the two entities.  Next, let&#8217;s assume that I wrote a beautiful client-side service layer that has both basic CRUD operations like create team and delete team, and more complex operations like trade player.  I&#8217;d like to cover everything with a set of tests so I can spend my time worrying about other things.</p>
<p class="bottom">Here&#8217;s a simple async test case for the basic create team operation:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createTeam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">createTeam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Los Angeles Lakers'</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>createTeam2, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createTeam2<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">getAllTeams</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>createTeam3, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createTeam3<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> teams:ArrayCollection = event.<span style="color: #006600;">result</span> as ArrayCollection;
    assertThat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Team not created'</span>, <span style="color: #ff0000;">'Los Angeles Lakers'</span>, inArray<span style="color: #66cc66;">&#40;</span>teams.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>First, we create a new team, then we load all the teams, and lastly, we verify that the newly created team is in the list.  There are two important things to note: async stuff is everywhere (<code>[Test(async)]</code> metadata, <code>AsyncToken</code>, <code>AsyncResponder</code>, etc.), and there is a chain of functions (<code>createTeam()</code> chains to <code>createTeam2()</code> which chains to <code>createTeam3()</code>).  In particular, the <b>chain</b> pattern is characteristic of any async testing.  Every single non-trivial async test involves a chain of function calls to do the work of testing an asynchronous backend.</p>
<p class="bottom">Here&#8217;s a simple diagram of the chain for the create team test:</p>
<div class="span-14 last" style="min-height:134px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/02/diagram_test-create-team.png" alt="create-team" width="528" height="124" /></div>
<p>Each diagram box is just a logic operation in our test, and they also happen to correspond exactly to the functions that make up the test chain.</p>
<h3>Test: Trade Player</h3>
<p>When testing the more complex client-side service layer operations, or simply writing more complex tests, the chain pattern often develops branches and sub-chains as various pieces of state are verified asynchronously.</p>
<p class="bottom">A good example is the trade player operation, which we might test using a chain with two branches: one to verify the player was removed from old team, and one to verify the player was added to the new team.  Here&#8217;s the diagram:</p>
<div class="span-14 last" style="min-height:292px;">
<img src="http://saturnboy.com/wp-content/uploads/2010/02/diagram_test-trade-player.png" alt="trade-player" width="528" height="282" /></div>
<p>We don&#8217;t really care what goes on inside the client-side service layer to achieve this, or even what happens on the backend (it&#8217;s probably just as simple as changing the <code>team_id</code> column on the <code>players</code> table to the new team&#8217;s id).  We only care that the test passes.</p>
<p class="bottom">And the accompanying test code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">tradePlayer</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Carmelo Anthony'</span>, <span style="color: #ff0000;">'Denver Nuggets'</span>, <span style="color: #ff0000;">'Cleveland Cavaliers'</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>tradePlayer2, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer2<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = service.<span style="color: #006600;">getPlayersByTeam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Denver Nuggets'</span><span style="color: #66cc66;">&#41;</span>;
    token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>tradePlayer3, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> token2:AsyncToken = service.<span style="color: #006600;">getPlayersByTeam</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Cleveland Cavaliers'</span><span style="color: #66cc66;">&#41;</span>;
    token2.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>Async.<span style="color: #006600;">asyncResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, <span style="color: #000000; font-weight: bold;">new</span> TestResponder<span style="color: #66cc66;">&#40;</span>tradePlayer4, fault<span style="color: #66cc66;">&#41;</span>, TIMEOUT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer3<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> players:ArrayCollection = event.<span style="color: #006600;">result</span> as ArrayCollection;
    assertThat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Traded player not removed from old team'</span>, <span style="color: #ff0000;">'Carmelo Anthony'</span>, <span style="color: #0066CC;">not</span><span style="color: #66cc66;">&#40;</span>inArray<span style="color: #66cc66;">&#40;</span>players.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tradePlayer4<span style="color: #66cc66;">&#40;</span>event:ResultEvent, passThroughData:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> players:ArrayCollection = event.<span style="color: #006600;">result</span> as ArrayCollection;
    assertThat<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Traded player not added to new team'</span>, <span style="color: #ff0000;">'Carmelo Anthony'</span>, inArray<span style="color: #66cc66;">&#40;</span>players.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The interesting part occurs in the second step in the chain, <code>tradePlayer2()</code>.  In this function, we use a pair of <code>AsyncToken</code>s, to fork the chain into two sub-chains.  One sub-chain gets all the players on the old team and verifies that the traded player has been removed.  And the other sub-chain gets all the players on the new team and verifies that the trade player has been added.</p>
<h3>A Better Approach</h3>
<p>Right now, the chained function approach is the only approach for testing an asynchronous client-side service layer.  As another example, you can see the chained function approach appears again when I tested an <a href="http://www.adobe.com/products/livecycle/dataservices/">LCDS</a>-powered backend in my <a href="http://www.insideria.com/2009/12/getting-real-with-lcds-3-beta.html">Getting Real with LCDS, Part 1</a> article at <a href="http://www.insideria.com/">InsideRIA.com</a>.</p>
<p>There has got to be something better, right?  Chained functions work fine, but boy are they ugly looking in code.  I&#8217;ve been having a <a href="http://forums.adobe.com/thread/568166">discussion</a> on the FlexUnit forums about better async testing.  The general wisdom is that one could use the <code>Sequence</code> interfaces to build an async action and have the <a href="http://docs.flexunit.org/asdocs/org/fluint/sequence/SequenceRunner.html"><code>SequenceRunner</code></a> manage the chain.  Currently, the best documentation on Sequences is the old <a href="http://code.google.com/p/fluint/wiki/Sequences">Fluint wiki doc</a>.  In enterprise Flex development, async backends tend to swarm like locusts, so I hope to have some code to show soon to streamline the testing process.</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flexunit4/test_basketball/TestBasketball.html">TestBasketball</a> (<a href="http://saturnboy.com/proj/flexunit4/test_basketball/TestRunner.html">run tests</a>, <a href="http://saturnboy.com/proj/flexunit4/test_basketball/srcview/index.html">view source</a>, <a href="http://saturnboy.com/proj/flexunit4/test_basketball/srcview/TestBaketball.zip">download</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/02/async-testing-with-flexunit4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
