My thought is that this is a good use-case and you should look into
proposing how a class should indicate asynchronous setup and teardown
and then go ahead and implement.  Then submit as a proposal :-)

One idea is that we could add a variable called ready to the TestCase
that is by default true.  We would then adjust the runMiddle() method to
check the ready variable and if it is true go ahead and call
runTestOrAsync.  If it is false it simply would wait and call itself
again a little later.  Then in your setup you set ready to false, and
when your data loads you set ready to true.

I haven't thought it all the way through, but something like this could
work.  Check the source out and maybe override some of those methods in
your own testcase to see how it works out.

Matt

-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of bringrags
Sent: Monday, March 06, 2006 2:02 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Asynchronous TestCase.setUp in FlexUnit

Hi,

I've been using FlexUnit with one of my projects and ran into a
problem.  In one of my test cases, I want to load a bunch of data from
an XML file  in the TestCase.setUp() method, and have all of the
individual tests use this as a fixture.

The problem is that loading a file is an asynchronous operation, but
there's no mechanism to make setUp asynchronous.  Consider the
following excerpt:

public function setUp():void
{
   var loader:URLLoader = new URLLoader();
   loader.addEventListener(Event.COMPLETE,onComplete);
   loader.load(new URLRequest("myfile.xml"));
}

private function onComplete(event:Event):void
{
   var loader:URLLoader = URLLoader(event.target);
   myXML = new XML(loader.data);
}

public function testSomething():void
{
   assertTrue("Should pass", myXML.someElement = "some value");
}

The problem is that testSomething() gets called before onComplete(). 
Ideally I would like to prevent the tests from running until the
onComplete() method is called.

FlexUnit provides support for having a chain of asynchronous tests,
but it doesn't seem to provide support for chaining asynchronous setup
tasks.  One possible workaround is to have each test method explicitly
prepare the setup fixture:

public function testSomething():void
{
   beginTestSetup(testSomethingHandler);
}

private function testSomethingHandler(event:Event):void
{
   endTestSetup(event);

   assertTrue("Should pass", xml.someElement = "some value");
}

private function beginTestSetup(eventHandler:Function):void
{
   var loader:URLLoader = new URLLoader();
   loader.addEventListener(Event.COMPLETE,addAsync(eventHandler,1000));
                        loader.load(new URLRequest("myfile.xml"));
}
                
private function endTestSetup(event:Event):void
{
   var loader:URLLoader = URLLoader(event.target);
   myXML = new XML(loader.data);
}

But this is less than ideal.  Any thoughts?

-- Brian





--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links



 




--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to