<?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; testing</title>
	<atom:link href="http://saturnboy.com/tag/testing/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>
		<item>
		<title>Testing async services with Fluint</title>
		<link>http://saturnboy.com/2009/03/fluint-async-testing/</link>
		<comments>http://saturnboy.com/2009/03/fluint-async-testing/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 04:58:45 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[flexmonkey]]></category>
		<category><![CDATA[fluint]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=307</guid>
		<description><![CDATA[We&#8217;re pretty big on testing at Gorilla Logic, and in the world of Flex that usually means using FlexMonkey to test the UI and using FlexUnit to test the code. Alas, it is a huge pain in the ass to correctly test the many async objects and services inherent in any Flex app with FlexUnit. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re pretty big on testing at <a href="http://www.gorillalogic.com/">Gorilla Logic</a>, and in the world of Flex that usually means using <a href="http://code.google.com/p/flexmonkey/">FlexMonkey</a> to test the UI and using <a href="http://opensource.adobe.com/wiki/display/flexunit/Flexunit">FlexUnit</a> to test the code.  Alas, it is a huge pain in the ass to correctly test the many async objects and services inherent in any Flex app with FlexUnit.  Enter <a href="http://code.google.com/p/fluint/">Fluint</a>, an superior Flex unit tesing framework by the cool guys at <a href="http://www.digitalprimates.net/">digital primates</a> (no relation).  Fluint is the heir apparent to take over the unit testing crown from the venerable FlexUnit.  So let&#8217;s take Fluint and its enhanced async testing support for a spin.</p>
<h5>Service Layer</h5>
<p class="bottom">First, assume we have a nice service layer in Flex that talks asynchronously to our backend.  Just something simple to start:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MyService <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> getSomething<span style="color: #000000;">&#40;</span>result<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span>, fault<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>AsyncToken <span style="color: #000000;">&#123;</span>
        <span style="color: #009900;">//call the backend</span>
        <span style="color: #6699cc; font-weight: bold;">var</span> token<span style="color: #000000; font-weight: bold;">:</span>AsyncToken = backend.getSomething<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #009900;">//wire the callbacks to the result</span>
        token.addResponder<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> AsyncResponder<span style="color: #000000;">&#40;</span>result, fault, token<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">return</span> token;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In this example, our service only has one method, <code>getSomething()</code> that takes two callback functions.  It simply calls the backend method, wires up the callbacks (which get called when the backend method returns a result), and returns the token.  It is <b>absolutely critical</b> that our callback-powered service method return the <code>AsyncToken</code>.  The reason for this will become apparent.</p>
<p class="bottom">We might use our service like this:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span></span>
<span style="color: #000000;">        xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #000000;">        creationComplete=<span style="color: #ff0000;">&quot;complete()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
    <span style="color: #339933;">&lt;mx:Script&gt;</span>
<span style="color: #339933;">    &lt;![CDATA[</span>
<span style="color: #339933;">        import com.saturnboy.services.MyService;</span>
<span style="color: #339933;">        private var service:MyService;</span>
&nbsp;
<span style="color: #339933;">        private function complete():void {</span>
<span style="color: #339933;">            service = new MyService();</span>
<span style="color: #339933;">            service.getSomething(resultHandler, faultHandler);</span>
<span style="color: #339933;">        }</span>
&nbsp;
<span style="color: #339933;">        private function resultHandler(result:Object, token:Object=null):void {</span>
<span style="color: #339933;">            lbl.text = result.result.name;</span>
<span style="color: #339933;">        }</span>
&nbsp;
<span style="color: #339933;">        public function faultHandler(error:Object, token:Object=null):void {</span>
<span style="color: #339933;">            lbl.text = 'fault';</span>
<span style="color: #339933;">        }</span>
<span style="color: #339933;">    ]]&gt;</span>
<span style="color: #339933;">    &lt;/mx:Script&gt;</span>
&nbsp;
    <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Label</span> id=<span style="color: #ff0000;">&quot;lbl&quot;</span> text=<span style="color: #ff0000;">&quot;initial&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>We make a call our service, and then use the callbacks to alter the UI however we want depending on the result.  In common usage, the fact that our service returns an <code>AsyncToken</code> is worthless, it might as well return <code>void</code>.  So, why did I say this is critical?  Throw Fluint testing into the mix and it&#8217;s &#8220;Show em what&#8217;s behind door number 2, Johnny!&#8221;</p>
<h5>Fluint Testing</h5>
<p class="bottom">Fluint provides two different async wrapper methods: <code>asyncHandler</code> and <code>asyncResponder</code>.  The first allows a test to be wired to an async method by events, the second allows a test to be wired to an async method by a responder.  Since the service method we&#8217;re trying to test doesn&#8217;t throw any events, we&#8217;ll need to use the latter.  So inside a Fluint test case, we have our test method:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> testGetSomething<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #009900;">//call service with dummy callback</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> token<span style="color: #000000; font-weight: bold;">:</span>AsyncToken = service.getSomething<span style="color: #000000;">&#40;</span>dummyResult, dummyFault<span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #009900;">//create async test responder</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> responder<span style="color: #000000; font-weight: bold;">:</span>IResponder = asyncResponder<span style="color: #000000;">&#40;</span>
            <span style="color: #0033ff; font-weight: bold;">new</span> TestResponder<span style="color: #000000;">&#40;</span>testHandler, faultHandler<span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">1000</span>, token<span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #009900;">//wire test responder as 2nd callback</span>
    token.addResponder<span style="color: #000000;">&#40;</span>responder<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> testHandler<span style="color: #000000;">&#40;</span>result<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, passThroughData<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
    assertEquals<span style="color: #000000;">&#40;</span><span style="color: #990000;">'something'</span>, result.result.<span style="color: #004993;">name</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The trick is to wire a second callback via Fluint&#8217;s <code>asyncResponder</code> helper that actually does the testing, and just give the original service call some dummy callbacks.  Note that if the service method didn&#8217;t return its <code>AsyncToken</code> there would be no way to wire a second callback.  The Fluint async helper do two import operations: they handle the event or call the callback AND they mark the test method as an async method so the result is correctly reported by the test harness.  You can read more about <a href="http://code.google.com/p/fluint/wiki/AsyncTest">Async Testing</a> in Fluint&#8217;s wiki.  The rest of Fluint is your standard chain of crap borrowed from JUnit: test runner, test suites, and test cases.</p>
<blockquote class="deeper"><p><b>Digging Deeper:</b> It is equally critical to use dummy callbacks in the original service method call because in a failure situation they will cause Flash Player to error out instead of being caught by Fluint and reported as a test failure.</p></blockquote>
<h5>Files</h5>
<p>The complete code is up on <a href="http://www.github.com/">GitHub</a> here: <a href="http://github.com/saturnboy/test_fluint_async/tree/master">test_fluint_async</a>.  The code is MIT licensed and includes a working fluint.swc (see below) plus a mock async backend (so timeouts and faults are easily testable).</p>
<p>Alas, Fluint v1.1.0 was built incorrectly and is missing the <code>TestResponder</code> class (see <a href="http://code.google.com/p/fluint/issues/detail?id=35&#038;can=1">issue 35</a>).  So if you want to try out Fluint in your project, I recommend you grab it from svn and build the swc yourself.  Hopefully, this will all be fixed in the next release.</p>
<p><strong>UPDATE:</strong> Fluint v1.1.1 was release on May 1, 2009 and fixes this issues and a few others.  Download it <a href="http://code.google.com/p/fluint/downloads/list">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2009/03/fluint-async-testing/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
