FlexUnit4 & Parsley
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.





August 17th, 2010 - 01:30
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
August 18th, 2010 - 00:34
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