BetaDesigns( Blog ) Flex and Component Development

29Apr/102

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.

  1. private var _testClass : MyTestPM;
  2.  
  3. [Before]
  4. public function setup() : void
  5. {
  6. _testClass = new MyTestPM();
  7. _testClass.myFirstInjectedItem = new MyFirstInjectedItem();
  8. _testClass.mySecondInjectedItem = new MySecondInjectedItem();
  9. _testClass.myFirstInjectedItem.mySecondInjectedItem =
  10. _testClass.mySecondInjectedItem();
  11. }

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.

  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <fx:Object xmlns:fx="http://ns.adobe.com/mxml/2009"
  4. xmlns:s="library://ns.adobe.com/flex/spark"
  5. xmlns:mx="library://ns.adobe.com/flex/mx"
  6. xmlns:spicefactory="http://www.spicefactory.org/parsley">
  7. <fx:Script>
  8. <![CDATA[
  9. import co.uk.betadesigns.domain.TestModel;
  10. import co.uk.betadesigns.domain.TestModel2;
  11. ]]>
  12. </fx:Script>
  13. <fx:Declarations>
  14. <spicefactory:Object type="{ TestModel }" />
  15. <spicefactory:Object type="{ TestModel2 }" />
  16. </fx:Declarations>
  17.  
  18. </fx:Object>
  19.  
  20.  

2. Next Create your TestClass.

  1. package flexUnitTests
  2. {
  3. import co.uk.betadesigns.domain.TestModel;
  4. import flexUnitTests.context.TestContext;
  5. import flexunit.framework.Assert;
  6. import org.spicefactory.parsley.core.context.Context;
  7. import org.spicefactory.parsley.flex.FlexContextBuilder;
  8.  
  9. public class InjectableTestCase
  10. {
  11. //Begin by injecting the class you want to test.
  12. [Inject]
  13. public var model : TestModel;
  14.  
  15. [Before]
  16. public function setUp() : void
  17. {
  18. //In the setup use the FlexContextBuilder to build
  19. //your testContext that you created in step 1.
  20. var cont : Context = FlexContextBuilder.build( TestContext );
  21. //Then add this test case to the context which will begin the
  22. //Injection Process. This will be called before every test
  23. //so your injected items will all be fresh and new.
  24. cont.createDynamicContext().addObject( this );
  25. }
  26.  
  27. [After]
  28. public function tearDown() : void
  29. {
  30. model = null;
  31. }
  32.  
  33. [Test]
  34. public function AddMultipleModels1() : void
  35. {
  36. //Simple tests to show that the models are re-create
  37. //every time a new test is run.
  38. Assert.assertEquals( "Should only ever have one count.",
  39. model.counter, 0 );
  40. Assert.assertEquals( "Should only ever have one count.",
  41. model.model2.counter, 0 );
  42. model.counter ++;
  43. model.model2.counter ++;
  44. Assert.assertEquals( "Should only ever have one count.",
  45. model.counter, 1 );
  46. Assert.assertEquals( "Should only ever have one count.",
  47. model.model2.counter, 1 );
  48. }
  49.  
  50. [Test]
  51. public function AddMultipleModels2() : void
  52. {
  53. //Simple tests to show that the models are re-create
  54. //every time a new test is run.
  55. Assert.assertEquals( "Should only ever have one count.",
  56. model.counter, 0 );
  57. Assert.assertEquals( "Should only ever have one count.",
  58. model.model2.counter, 0 );
  59. model.counter ++;
  60. model.model2.counter ++;
  61. Assert.assertEquals( "Should only ever have one count.",
  62. model.counter, 1 );
  63. Assert.assertEquals( "Should only ever have one count.",
  64. model.model2.counter, 1 );
  65. }
  66. }
  67. }

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.

Be Sociable, Share!
Comments (2) Trackbacks (1)
  1. 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

  2. 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


Leave a comment

(required)