--- In flexcoders@yahoogroups.com, Kevin <[EMAIL PROTECTED]> wrote:
>
> Is anyone currently using FlexUnit to test a Cairngorm app?  I 
have  
> some questions about how best to set this up.
> 
> Thanks, Kevin
>

Yes, people are using FlexUnit to test Cairngorm apps.  There are at 
least two different ways to do it.  The first is detailed here:

http://jharbs.com/blog/
http://flexapps.blogspot.com/2006/11/cairngorm-iresponder-async-flexunit.html

My understanding is that either one or both of Cairngorm and 
Flexunit are being changed so that they work together better. As a 
temporary solution, I came up with something that's not nearly as 
efficient as it should be, but allowed me to test my Cairngorm apps 
without making any modifications to FlexUnit.

The Quick summary is:

1. Dispatch your event
2. Create a closure that will verify the command has correctly 
modified the model.
3. Tell flexunit you wan't that closure as the async handler

And here's some sample code:

public function testMyEventvoid
{
  var event:SampleEvent = new SampleEvent(); 
  CairngormEventDispatcher.getInstance().dispatchEvent(event);

  // create a closure that will be used to test the object
  // once the appropriate command has been executed
  var f:Function = function(e:*):void
  {
    // make sure that it was called correctly
    assertEquals("MyValue", model.value);
    assertTrue(event.someProperty);
  }
  callbackHelper(f);
}

// tell FlexUnit we're listening for an async event to happen
private function callbackHelper(func:Function, runIn:uint=10, 
timeout:uint=50) : void
{
  var t:Timer = new Timer(runIn, 1);
  t.addEventListener(TimerEvent.TIMER_COMPLETE, addAsync(func, 
timeout));
  t.start();
}

What's bad about the above is you have to wait until you're pretty 
sure the command will have executed, and then perform the tests.  
So, if it takes 5 ms to perform the command, you might have to wait 
another 200 ms before you actually perform the test.

I hope that helps.

Kaleb Pederson
http://www.soph-ware.com/

Reply via email to