Search code examples
apache-flexflexunit

instantiating view won't work


Hey guys!
We used to write our UnitTests with FlexUnit and we were just testing our model. Now we want to test the view too. Before i run my tests i create an instance of my view and my model to test the stuff. When i try to access the view i get a null pointer exception. If i add the view to the displaylist it somehow works - even if i remove it from the list right after adding.

it looks something like this:

var myView: MyView = new myView();
//myView.initialize(); will throw error
Application.application.addChild(view);
Application.application.removeChild(view);
myView.initialize(); // will work


Hope you can give me a hint.

Sims


Solution

  • Flex UIComponents do not walk through the component lifecycle until after they are added to a container. As such, variables may not be initialized and children may not be created if you never add it to a container.

    More info on the Flex Component LifeCycle. You'll notice there are 11 steps after the component is added to the container.

    I suspect that adding it, then removing it, could cause other issues but it depends what you're trying to test.

    To know your exact error, we'd have to see what code is in the initialize method on the view. Most likely it accesses a child that was not created yet.

    MXML components will often masks the lifecycle steps, but a component will still go through them.

    I hope this helps; but since you didn't ask a question I'm not sure if that was the information you were after.