I am trying to test some of my AS classes in flexunit. Some of the methods reference the stage from components that are passed to it. When I set up the flexunit testing application I use the following code:
core = new FlexUnitCore();
core.visualDisplayRoot = this;
core.addListener( new UIListener( uiListener ));
However, I still get a null reference exception whenever the stage is referenced. I was wonding if anyone knew what is going on or what I am doing wrong.
The previous code is in the creationComplete event function for my test runner. (I used the flexunit 4.1 turnkey example)
The "this" is the runner application.
Then in the test I would have:
public function testFunct():void
//this class stores various elements so that other classes can referance them
//this fails when testing
I am not sure why the assert equals null.
Okay, a few things.
If you are using Flex, you don't need to set the visualDisplayRoot. That is for ActionScript only projects.
Second, display objects in Flash don't have access to the stage until they are on the display list. So, saying
new Image() won't add that item to the display list.
I am not sure what Control.initApp() does, but in one of your unit tests, they way you get something on the display list is to use the UIImpersonator:
UIImpersonator.addChild( someImage );
After that someImage would have access to the stage. However, bigger problem. Unit tests are about isolation and if you components need to get out to the stage or if Control.initApp() somehow adds the item directly to the display list you are going to continue to have different issues as you won't be able to keep things in isolation to test.
Hope that clarifies a little bit,
Europe, Middle East and Africa