任何人都知道 Async.asyncHandler()
是如何工作的,如果 Async.processOnEvent()
只能在 [Before] 方法中使用。(任何人都知道一些有用的除了 http://docs.flexunit.org/ 之外的文档)。
我定义了一个名为 HelloCompo(extends Vbox) 的 MXML 组件,该组件定义了一个名为 hello() 的函数,在 hello() 中分发了一个名为 HelloEvent 的客户事件(事件类型刚刚命名为“hello”),并在另一个名为 init() 的函数监听了该事件,我想测试该事件是否正确分派(dispatch)。所以我有以下测试:
var helloCompo = new HelloCompo();
helloCompo.hello();
helloCompo.addEventListener("hello", Async.asyncHandler(this, handleHello, 1000, null, handleTimeOut));
测试总是会执行handleTimeOut方法(意思是HelloEvent没有被dispatch,但是当helloCompo.hello()执行的时候真的dispatched了,怎么回事?)
最佳答案
package flexUnitTests
{
import flash.events.Event;
import org.flexunit.asserts.assertTrue;
import org.flexunit.asserts.fail;
import org.flexunit.async.Async;
public class HelloTest
{
private var helloCompo:HelloCompo;
[Before]
public function setUp():void
{
helloCompo = new HelloCompo();
}
[After]
public function tearDown():void
{
helloCompo = null;
}
[Test(async)]
public function testHello():void
{
var handler:Function = Async.asyncHandler(this, helloHandler, 300, null, helloFailed);
helloCompo.addEventListener("hello", handler);
helloCompo.hello();
}
private function helloHandler(event:Event, passThroughObject:Object):void
{
//assert somthing
}
private function helloFailed(event:Event, passThroughObject:Object):void
{
fail("hello not dispatched");
}
}
}
HelloCompo.as
package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
public class HelloCompo extends EventDispatcher
{
public function HelloCompo(target:IEventDispatcher=null)
{
super(target);
}
public function hello():void
{
dispatchEvent(new Event("hello"));
}
}
}
关于apache-flex - 处理客户事件的flexunit和Async.asyncHandler(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291763/