Flex 单元 aysnc 问题:错误:异步事件接收无序
我正在编写测试用例来使用 flexunit 4 测试功能。我正在使用 aysnc 方法。 但是当我向实例添加两个或多个 asyncHandler 时。我遇到问题:错误:异步事件接收无序。如何解决这个问题?谢谢。
代码片段:
[Test(order=1, async, description="synchronize content on line")]
public function testSynchronizeContentOnline():void
{
var passThroughData:Object = new Object();
var asyncHandler1:Function = Async.asyncHandler(this, authFailureHandler, 60000, null, timeoutHandler);
var asyncHandler:Function = Async.asyncHandler(this, authSuccessHandler, 60000, null, timeoutHandler);
caseManager.addEventListener(CaseAuthEvent.AUTH_SUCCESS,
asyncHandler);
caseManager.addEventListener(CaseAuthEvent.AUTH_FAILURE,
asyncHandler1);
caseManager.authenticate("admin", "admin");
trace('test');
}
private function timeoutHandler(event:Event):void
{
Assert.fail( "Timeout reached before event");
}
private var authFailed:Boolean = false;
private function authFailureHandler(event:CaseAuthEvent, passThroughData:Object):void
{
trace("authFailure:" + event.type);
authFailed = true;
}
private var authSucceed:Boolean = false;
private function authSuccessHandler(event:CaseAuthEvent, passThroughData:Object):void
{
trace("authSucceed:" + event.type);
authSucceed = true;
Assert.assertTrue(true);
}
I am writing test cases to test function with flexunit 4. I am using aysnc method.
But when I add two or more asyncHandlers to the instance. I meet the problem: Error: Asynchronous Event Received out of Order. How to resolve this problem? Thanks.
Code snippets:
[Test(order=1, async, description="synchronize content on line")]
public function testSynchronizeContentOnline():void
{
var passThroughData:Object = new Object();
var asyncHandler1:Function = Async.asyncHandler(this, authFailureHandler, 60000, null, timeoutHandler);
var asyncHandler:Function = Async.asyncHandler(this, authSuccessHandler, 60000, null, timeoutHandler);
caseManager.addEventListener(CaseAuthEvent.AUTH_SUCCESS,
asyncHandler);
caseManager.addEventListener(CaseAuthEvent.AUTH_FAILURE,
asyncHandler1);
caseManager.authenticate("admin", "admin");
trace('test');
}
private function timeoutHandler(event:Event):void
{
Assert.fail( "Timeout reached before event");
}
private var authFailed:Boolean = false;
private function authFailureHandler(event:CaseAuthEvent, passThroughData:Object):void
{
trace("authFailure:" + event.type);
authFailed = true;
}
private var authSucceed:Boolean = false;
private function authSuccessHandler(event:CaseAuthEvent, passThroughData:Object):void
{
trace("authSucceed:" + event.type);
authSucceed = true;
Assert.assertTrue(true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为您正在向测试用例添加顺序,这似乎在第一个测试用例完成之前正在调度其他东西。引用 Flex 单元 wiki 的订购部分:
我完全同意这一点。您的测试中不应该有任何顺序。测试本身设定了需要完成的工作的状态。
That would be because you're adding order to your test cases, which is seems something else is dispatching before the first one is complete. To quote the ordering part of the flex unit wiki:
Which I completely agree with. There should not be any order in your tests. The tests themselves sets the state of what needs to be done.
如果您分别测试成功和失败,您的测试就会有效。所以基本上有两个测试,一个为事件成功添加一个异步处理程序,另一个为事件失败添加一个异步处理程序。这是我将进行的两个测试的示例...
请记住在设置函数中创建 caseManager 的新实例,并且最好的做法是在拆解中删除对其的引用,如简单的代码片段所示,我刚刚假设 caseManager 是 CaseManager 类型。
Your test will work if you test success and fail separately. So basically have 2 tests, one adds an async handler for your events success, the other for the events fail. Here is an example of the 2 tests as I would approach them...
Remember to make a new instance of your caseManager in your set up function and its good practice to remove ref to it in the tearDown as the simple code snippet shows, I've just assumed the caseManager is of type CaseManager.