处理客户事件的Flexunit和async.asynchandler()
-
28-09-2019 - |
题
任何人都知道如何 Async.asyncHandler()
工作和如果 Async.processOnEvent()
只能在[之前]方法中使用。(任何人都知道一些有用的文档 http://docs.flexunit.org/).
我定义了一个名为hellocompo(扩展Vbox)的MXML组件,并且该组件在Hello()中定义了名为Hello()的函数,分配了一个名为HelloEvent的客户事件(事件类型恰好名为“ Hello hello”),并在另一个名为名为的函数中init()收听了事件,我想测试该事件是否正确派遣。因此,我的测试以下是:
var hellocompo = new hellocompo();
helloCompo.hello();
helloCompo.addEventListener("hello", Async.asyncHandler(this, handleHello, 1000, null, handleTimeOut));
该测试总是会引起Handletimeout方法(意味着HelloEvent没有派遣,但是当Hellocompo.hello()concute concute时,它确实分配了,所以怎么了?)
解决方案
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"));
}
}
}
其他提示
我认为您需要在调用Hello()之前添加活动侦听器
不隶属于 StackOverflow