SequenceWaiter无法识别我的事件

时间:2011-07-07 08:40:21

标签: flex flexunit parsley

我正在使用此AsyncHelper。我想创建基本的测试用例,我想从测试方法派遣事件(通过Parsley MessageDispatcher),在任何其他方法中捕获此事件并将其分发回来(通过flex EventDispatcher)。

这是我的测试用例: package flexUnitTests { import flash.events.ErrorEvent; import flash.events.Event; import flash.events.EventDispatcher;

import flexUnitTests.events.TestEvent;
import flexUnitTests.helpers.AsyncHelper;

import mx.logging.Log;
import mx.logging.LogEventLevel;
import mx.logging.targets.TraceTarget;

import org.flexunit.assertThat;
import org.hamcrest.object.equalTo;
import org.spicefactory.parsley.core.context.Context;
import org.spicefactory.parsley.core.messaging.MessageProcessor;
import org.spicefactory.parsley.dsl.context.ContextBuilder;
import org.spicefactory.parsley.flex.FlexConfig;
import org.spicefactory.parsley.flex.FlexContextBuilder;

public class HelloTest extends AsyncHelper {

    [MessageDispatcher]
    public var dispatcher:Function;

    [Inject]
    public var context:Context;

    public var eventDispatcher:EventDispatcher;

    [Before]
    public function initializeContext():void {
        // Use ContextBuilder to build context
        var context:Context = ContextBuilder.newSetup()
            .newBuilder()
            .config(FlexConfig.forClass(ParsleyConfig))
            .build();

        // Add this test case to context 
        context.addDynamicObject(this);

        eventDispatcher = new EventDispatcher();
    }

    [After]
    public function destroyContext():void {
        eventDispatcher = null;
    }

    [MessageHandler(selector='TestEvent.ok')]
    public function passItOn(event:TestEvent):void {
        eventDispatcher.dispatchEvent(event);
    }

    [Test(async)]
    public function tryIt():void {
        dispatcher(new TestEvent(TestEvent.OK));
        waitFor(eventDispatcher, TestEvent.OK, 2000);
        thenAssert(function(event:TestEvent, data:*):void {
            assertThat(event.type, equalTo('TestEvent.ok'));
        });
    }
}

import flexUnitTests.events.TestEvent; import flexUnitTests.helpers.AsyncHelper; import mx.logging.Log; import mx.logging.LogEventLevel; import mx.logging.targets.TraceTarget; import org.flexunit.assertThat; import org.hamcrest.object.equalTo; import org.spicefactory.parsley.core.context.Context; import org.spicefactory.parsley.core.messaging.MessageProcessor; import org.spicefactory.parsley.dsl.context.ContextBuilder; import org.spicefactory.parsley.flex.FlexConfig; import org.spicefactory.parsley.flex.FlexContextBuilder; public class HelloTest extends AsyncHelper { [MessageDispatcher] public var dispatcher:Function; [Inject] public var context:Context; public var eventDispatcher:EventDispatcher; [Before] public function initializeContext():void { // Use ContextBuilder to build context var context:Context = ContextBuilder.newSetup() .newBuilder() .config(FlexConfig.forClass(ParsleyConfig)) .build(); // Add this test case to context context.addDynamicObject(this); eventDispatcher = new EventDispatcher(); } [After] public function destroyContext():void { eventDispatcher = null; } [MessageHandler(selector='TestEvent.ok')] public function passItOn(event:TestEvent):void { eventDispatcher.dispatchEvent(event); } [Test(async)] public function tryIt():void { dispatcher(new TestEvent(TestEvent.OK)); waitFor(eventDispatcher, TestEvent.OK, 2000); thenAssert(function(event:TestEvent, data:*):void { assertThat(event.type, equalTo('TestEvent.ok')); }); } }

我的活动看起来像这样: }

package flexUnitTests.events { import flash.events.Event;

public class TestEvent extends Event
{
    static public const OK :String = "TestEvent.ok";

    public function TestEvent(type:String)
    {
        super(type);
    }
}

我不知道问题出在哪里。如果我在代码中创建了跟踪,我可以看到Parsley将我的事件调度到passItOn方法中。并将flex EventDispatcher调度事件返回。但我不知道为什么SequenceWaiter不能识别我的事件。

1 个答案:

答案 0 :(得分:0)

我现在看到问题出在哪里: 这是因为我在创建序列之前调用了dispatcher()。我改变调用方法的顺序问题消失了。