Recreating a bunch of components for flex unit testing (flexunit)

355 views Asked by At

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.

1

There are 1 answers

4
Sunil D. On BEST ANSWER

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();
    }
}