0

我正在编写测试用例来测试 flexunit 4 的功能。我正在使用 aysnc 方法。但是当我向实例添加两个或更多 asyncHandlers 时。我遇到了问题:错误:异步事件接收错误。如何解决这个问题?谢谢。

代码片段:

[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);

    }
4

2 回答 2

0

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;
}
于 2011-12-14T16:22:43.480 回答
0

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

您的测试需要彼此独立运行,因此以自定义方式排序测试的目的不是确保测试 A 设置测试 B 需要的某些状态。如果这是您阅读本节的原因,请重新考虑。测试需要相互独立,并且通常独立于顺序。

我完全同意。你的测试不应该有任何顺序。测试本身设定了需要做什么的状态。

于 2011-09-08T03:39:33.600 回答