<?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>foomonger&#039;s blog &#187; mocking</title>
	<atom:link href="http://blog.foomonger.com/tag/mocking/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.foomonger.com</link>
	<description>Tips and Musings on Software Development, Flash, and Flex</description>
	<lastBuildDate>Wed, 05 Jan 2011 18:08:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mocking Loader and Services</title>
		<link>http://blog.foomonger.com/2010/01/30/mocking-loader-and-services/</link>
		<comments>http://blog.foomonger.com/2010/01/30/mocking-loader-and-services/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 17:54:05 +0000</pubDate>
		<dc:creator>Sam Ahn</dc:creator>
				<category><![CDATA[flex]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.foomonger.com/?p=85</guid>
		<description><![CDATA[I've been using mock-as3 in my Flex unit tests a lot recently discovered a couple useful strategies for mocking things that are not obviously mockable at first glance:  Loader and Services (AsyncToken).
Here is the source for a Flex project that I'll be referencing that contains a sample controller with its tests: MockingLoaderAndServices
Most of the relevant [...]]]></description>
			<content:encoded><![CDATA[<p>I've been using <a href="http://code.google.com/p/mock-as3/" target="_blank">mock-as3</a> in my Flex unit tests a lot recently discovered a couple useful strategies for mocking things that are not obviously mockable at first glance:  Loader and Services (AsyncToken).</p>
<p>Here is the source for a Flex project that I'll be referencing that contains a sample controller with its tests: <a href="http://blog.foomonger.com/pub/2010/01/30/mocking-loader-and-services/srcview/" target="_blank">MockingLoaderAndServices</a></p>
<p>Most of the relevant code is in com.foomonger.blog.mockingloaderandservices.controller.ControllerTest.</p>
<p>ControllerTest contains tests for Controller which has 2 methods: loadAssetsSwf() and loadDataXml(). Those methods call corresponding methods in the Delegate. loadAssetsSwf() demonstrates loading a SWF using a Loader. loadDataXML() demonstrates loading XML through an HTTPService. In ControllerTest I create MockDelegates which is then injected into the Controllers.</p>
<h2>flash.display.Loader</h2>
<p>When loading a SWF into another SWf, you use the Loader class and listen for events from the contentLoaderInfo property. To mock the loading of a SWF, you have to simulate events being dispatched from contentLoaderInfo. The problem is that you can't call dispatchEvent() nor subclass LoaderInfo and instantiate it.</p>
<p>The solution: LoaderInfo2. In the source you'll see that the class flash.display.Loader2  subclasses Loader and adds a contentLoaderInfo2 property of type IEventDispatcher. By default it's set to the normal contentLoaderInfo, but can be set to a generic EventDispatcher for testing. When you set contentInfoLoader2 to an EventDispatcher instance, you can then call dispatchEvent() on it. Note that Loader2 is used instead of Loader in the Delegate and Controller. See ControllerTest.test_loadAssetsSwf_complete() .</p>
<p>This is not an ideal solution but works well enough for my uses.</p>
<h2>mx.rpc.AsyncToken</h2>
<p>When loading data using Flex service objects, you get back an AsyncToken which you can add event listeners and responders. I typically add responders. To mock the service calls, you have to simulate the calling of the responders. The problem is how do you trigger the response?</p>
<p>After a little digging around, I found that ResultEvent and FaultEvent have the mx_internal method callTokenResponders() which triggers the response. So the solution is pretty straight-forward: create the appropriate Result(Fault)Event object, passing an AsyncToken,  and call callTokenResponders() .  See ControllerTest.test_loadDataXML_result().</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.foomonger.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.foomonger.com/2010/01/30/mocking-loader-and-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

