1

I have this App.mxml:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:Button id="bt" label="click me"/>
</s:Application>

And this test case:

package flexUnitTests
{
    import flexunit.framework.Assert;

    public class AppTest
    {       
        [Test]
        public function testApp():void
        {
            var app:App = new App();
            Assert.assertNotNull(app.bt);
        }
    }
}   

But app.bt is null. I want to access the button :(

Fabricio
  • 7,705
  • 9
  • 52
  • 87
  • I'm not sure why you'd want to do that. I don't really see the point of unit testing views. If you want to test your UI, there are other tools for that, as I've answered to your [other question](http://stackoverflow.com/questions/13860075/unit-testing-questions). – RIAstar Dec 13 '12 at 23:43

1 Answers1

1

Short Answer:

The life cycle methods have not run on app; so no createChildren() method was executed in order to create the child component of bt.

Longer Answer:

Things get slightly more complicated with the main application file, as it there is no higher level Flex component in the display hierarchy. I'm unclear on all specifics, but..

I think the Flex Compiler does some magic to set up this component--and the Flex Framework--that help makes the whole app work. You are, in essence, bypassing that work by creating your own instance of the component.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • 1
    Well, I guess you could manually call createChildren() commitProperties(), measure() and updateDisplayList(). There is a chance that could cause other unexpected issues and you'll have to end up creating your own system Manager or somehow initializing the Flex one and using that. For UI testing, I much prefer tools like RIATest or FlexMonkey. – JeffryHouser Dec 13 '12 at 17:00