Flex 单元 aysnc 问题:错误:异步事件接收无序

发布于 2024-12-03 12:03:32 字数 1422 浏览 5 评论 0原文

我正在编写测试用例来使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尬尬 2024-12-10 12:03:32

那是因为您正在向测试用例添加顺序,这似乎在第一个测试用例完成之前正在调度其他东西。引用 Flex 单元 wiki 的订购部分:

您的测试需要彼此独立运行,因此重点是
以自定义方式排序测试并不是为了确保测试 A 集
测试 B 需要的一些状态。如果这是您阅读的原因
这一部分,请重新考虑。测试需要独立于每个
其他且通常与顺序无关。

我完全同意这一点。您的测试中不应该有任何顺序。测试本身设定了需要完成的工作的状态。

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:

Your tests need to act independently of each other, so the point of
ordering your tests in a custom way is not to ensure that test A sets
up some state that test B needs. If this is the reason you are reading
this section, please reconsider. Tests need to be independent of each
other and generally independent of order.

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.

流殇 2024-12-10 12:03:32

如果您分别测试成功和失败,您的测试就会有效。所以基本上有两个测试,一个为事件成功添加一个异步处理程序,另一个为事件失败添加一个异步处理程序。这是我将进行的两个测试的示例...

[Test(async)]
public function  testEventSuccess():void
{
    var passThroughData:Object = new Object();

    var asyncHandler:Function = Async.asyncHandler(this, authSuccessHandler, 60000, null, timeoutHandler);

    caseManager.addEventListener(CaseAuthEvent.AUTH_SUCCESS, 
        asyncHandler);

    caseManager.authenticate("admin", "admin");
}

[Test(async)]
public function  testEventFailure():void
{
    var passThroughData:Object = new Object();

    var asyncHandler:Function = Async.asyncHandler(this, authFailureHandler, 60000, null, timeoutHandler);

    caseManager.addEventListener(CaseAuthEvent.AUTH_FAILURE, 
        asyncHandler);
    caseManager.authenticate("admin", "admin");
}

请记住在设置函数中创建 caseManager 的新实例,并且最好的做法是在拆解中删除对其的引用,如简单的代码片段所示,我刚刚假设 caseManager 是 CaseManager 类型。

[Before]
public function setUp():void
{
    caseManager = new CaseManager();
}

[After]
public function tearDown():void
{
    caseManager = null;
}

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...

[Test(async)]
public function  testEventSuccess():void
{
    var passThroughData:Object = new Object();

    var asyncHandler:Function = Async.asyncHandler(this, authSuccessHandler, 60000, null, timeoutHandler);

    caseManager.addEventListener(CaseAuthEvent.AUTH_SUCCESS, 
        asyncHandler);

    caseManager.authenticate("admin", "admin");
}

[Test(async)]
public function  testEventFailure():void
{
    var passThroughData:Object = new Object();

    var asyncHandler:Function = Async.asyncHandler(this, authFailureHandler, 60000, null, timeoutHandler);

    caseManager.addEventListener(CaseAuthEvent.AUTH_FAILURE, 
        asyncHandler);
    caseManager.authenticate("admin", "admin");
}

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.

[Before]
public function setUp():void
{
    caseManager = new CaseManager();
}

[After]
public function tearDown():void
{
    caseManager = null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文