0

I have a bunch of NumericSteppers (start week, start year, end week, end year), which are deep within ViewStacks, NavigatorContents etc. I wanted to unit test my date steppers, and was wondering how I can go about doing that? When I initialize the top level parent component, the children components don't get created. Do I have to manually add all these components by iterating down the tree (please say no :) )? Can I do it using UIImpersonator?

Sorry if the question is basic, Flex is very new to me.

iman453
  • 9,105
  • 16
  • 54
  • 70
  • 1
    "When I initialize the top level parent component, the children components don't get created" Without seeing your component, it's hard to give you any definitive advice. But, this could be due to the creationPolicy. http://livedocs.adobe.com/flex/3/html/help.html?content=layoutperformance_05.html Set creationPolicy to 'all' on your viewStacks and other navigators and all children should be created. – JeffryHouser Aug 03 '12 at 00:31

1 Answers1

1

In Flash, creating unit tests for GUI components is problematic. I generally write unit tests for controllers, presentation models, mediators (etc) -- ie: the non GUI classes that contain business logic.

Writing tests for GUI objects becomes a losing proposition, for many reasons:

  • the view's logic tends to need to be triggered by user interaction
  • the view may depend on low level Flash API's (NetStream, Camera, etc) that are difficult to simulate/mock in tests)
  • running tests that have GUI elements (things that use the stage or that you add to the stage) is not possible when running tests automatically (ie: kicked off by your continuous integration or build system)
  • tests tend to run slower

I generally avoid writing unit tests for components like a date stepper, which we compose together to form the greater "view". I typically use a presentation model, and if the component has particular business logic that should be tested, the tests are written for the non-gui presentation model class (or controller, or mediator, or whatever).

public class MyViewPM
{
   // write a unit test for this method
   public function onSubmitButtonClick():void
   {
   }
}

public class MyView extends Sprite
{
    // this is injected by your MVC framework
    // or set when the the view is created, or added to stage, etc.
    public var pm:MyViewPM;

    public function MyView()
    {
        submitButton.addEventListener(MouseEvent.Click, onMouseClick);
    }

    private function onMouseClick(event:Event):void
    {
        pm.onSubmitButtonClick();
    }
}
Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • There are plenty of tools for UI Testing of Flex components including RIATest, and FlexMonkey. I know Flexmonkey can create scripts that can be triggered as part of automated testing. In theory, Mustella--part of Apache Flex--can be used in the same way. That is exactly how the Flex Framework is tested; although Adobe has not yet committed their Mustella tests yet. – JeffryHouser Aug 03 '12 at 01:12
  • I would call FlexMonkey and RIATest GUI automation tools, not necessarily unit testing tools :) I'm not arguing against testing your GUI or building block components. But I believe for unit tests to be useful they should be isolated, easy to create/maintain, and not approach system/integration level type of tests (that GUI automation tends to do). – Sunil D. Aug 03 '12 at 04:06
  • 1
    D Whether or not the GUI can be tested in isolation of "system/integration" type of tests depends on how it is coded. In my own experience with client projects; I agree any sort of UI test would be more akin to integration testing than unit testing. However, if coded with modularization in mind--such as Flex Framework visual components or Flextras components--there is no reason why UI Components can't be unit tested. – JeffryHouser Aug 03 '12 at 04:43
  • Good points :) I was stuck in my box and perhaps not thinking outside of it. – Sunil D. Aug 03 '12 at 17:37