Currently Reading

Image of Derivatives Demystified: A Step-by-Step Guide to Forwards, Futures, Swaps and Options (The Wiley Finance Series)

Image of Working Effectively with Legacy Code (Robert C Martin)

Image of Coders at Work: Reflections on the Craft of Programming

Image of Succeeding with Agile: Software Development Using Scrum (Addison-Wesley Signature)

Image of iPhone UK: The Missing Manual

Image of My Shit Life So Far

Recently Read

Image of Advanced Actionscript 3 with Design Patterns

Image of The Secrets of Consulting: A Guide to Giving and Getting Advice Successfully

Image of Test Driven Development (The Addison-Wesley Signature Series)

Image of Test Driven: TDD and Acceptance TDD for Java Developers

Image of Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)

Image of Refactoring: Improving the Design of Existing Code (Object Technology Series)

Image of ActionScript 3.0 Design Patterns: Object Oriented Programming Techniques (Adobe Developer Library)

Image of iPhone Advanced Projects (Books for Professionals by Professionals)

Image of UML 2 for Dummies

Image of Cloud Atlas


April 29, 2010

FlexUnit4 & Parsley

Filed under: Flex, Test Driven Design — Anthony @ 11:14 pm

For the last six months I have been working on a rather large enterprise application that uses parsley as it's main Dependency Injection Framework. This has led to many complex class' that contain multiple injected models, VO and other elements. Recreating these items inside test harness can become very cumbersome if you have to create a large injection heirarchy. Consider the following example.

private var _testClass : MyTestPM;
 
[Before]
public function setup() : void
{
   _testClass = new MyTestPM();
   _testClass.myFirstInjectedItem = new MyFirstInjectedItem();
   _testClass.mySecondInjectedItem = new MySecondInjectedItem();
   _testClass.myFirstInjectedItem.mySecondInjectedItem =
         _testClass.mySecondInjectedItem();
}

As you can see this can become complicated with the more injected items you have that can then have references to other injected items!

However there is a way to use the Parsley DI Framework to do all that work for you!.

1. Create a TestContext File that contains all the Injectable objects your test class/suite needs.

 
<?xml version="1.0" encoding="utf-8"?>
<fx:Object xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:mx="library://ns.adobe.com/flex/mx"
	xmlns:spicefactory="http://www.spicefactory.org/parsley">
	<fx:Script>
		<![CDATA[
			import co.uk.betadesigns.domain.TestModel;
			import co.uk.betadesigns.domain.TestModel2;
		]]>
	</fx:Script>
	<fx:Declarations>
		<spicefactory:Object type="{ TestModel }" />
		<spicefactory:Object type="{ TestModel2 }" />
	</fx:Declarations>
 
</fx:Object>
 

2. Next Create your TestClass.

package flexUnitTests
{
   import co.uk.betadesigns.domain.TestModel;
   import flexUnitTests.context.TestContext;
   import flexunit.framework.Assert;
   import org.spicefactory.parsley.core.context.Context;
   import org.spicefactory.parsley.flex.FlexContextBuilder;
 
public class InjectableTestCase
{
//Begin by injecting the class you want to test.
[Inject]
public var model : TestModel;
 
[Before]
public function setUp() : void
{
	//In the setup use the FlexContextBuilder to build
	//your testContext that you created in step 1.
	var cont : Context = FlexContextBuilder.build( TestContext );
	//Then add this test case to the context which will begin the
	//Injection Process. This will be called before every test
	//so your injected items will all be fresh and new.
	cont.createDynamicContext().addObject( this );
}
 
[After]
public function tearDown() : void
{
	model = null;
}
 
[Test]
public function AddMultipleModels1() : void
{
	//Simple tests to show that the models are re-create
	//every time a new test is run.
	Assert.assertEquals( "Should only ever have one count.",
		model.counter, 0 );
	Assert.assertEquals( "Should only ever have one count.",
		model.model2.counter, 0 );
	model.counter ++;
	model.model2.counter ++;
	Assert.assertEquals( "Should only ever have one count.",
		model.counter, 1 );
	Assert.assertEquals( "Should only ever have one count.",
		model.model2.counter, 1 );
}
 
[Test]
public function AddMultipleModels2() : void
{
	//Simple tests to show that the models are re-create
	//every time a new test is run.
	Assert.assertEquals( "Should only ever have one count.",
		model.counter, 0 );
	Assert.assertEquals( "Should only ever have one count.",
		model.model2.counter, 0 );
	model.counter ++;
	model.model2.counter ++;
	Assert.assertEquals( "Should only ever have one count.",
		model.counter, 1 );
	Assert.assertEquals( "Should only ever have one count.",
		model.model2.counter, 1 );
}
}
}

Here is an example FlashBuilder Project to show this in action.
Hope this relives some stress when unit testing.

Of course you should remember that if you have hundreds or even thousands of tests that this probably isn't an ideal solution as parsley will parse the context file each and every time a test is run and although the overhead is minimal it will add up if you have soo many tests.

3 Comments »

  1. [...] More Posted by amccormick in ActionScript, Adobe Flex, Flex, RIA, Testing / Digg this / Add Comment » [...]

    Pingback by FlexUnit4 & Parsley » Lab49 Blog — May 10, 2010 @ 11:37 pm

  2. Hi mate, i was wondering, do you have an example which tests commands as well? cant find any decent example on that.

    Thanks a lot

    Comment by Doron Katz — August 17, 2010 @ 1:30 am

  3. Hi Doron,
    Sorry mate i have not tried to do that using parsley as I don’t currently use the Cairngorm Command Architecture.
    If you find any good examples please post them here as I would be interested in seeing them
    Regards
    Anthony

    Comment by Anthony — August 18, 2010 @ 12:34 am

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .