<?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; fluint</title>
	<atom:link href="http://saturnboy.com/tag/fluint/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>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>
