I'm using this AsyncHelper. I would like to create basic test case where I would like to dispatch event from the test method (by Parsley MessageDispatcher), catch this event in any other method and dispatch it back (by flex EventDispatcher).
This is my test case:
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.MessageP开发者_JAVA技巧rocessor;
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'));
});
}
}
}
And my Event looks like this:
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);
}
}
}
I don't know where is a problem. If I made traces at code, I can see that Parsley dispatch my event into passItOn method. And flex EventDispatcher dispatch event back. But I don't know why SequenceWaiter doesn't recognize my event.
I see where is a problem now: It's because I call dispatcher() before creating sequence. It I change order of calling methods problem disappear.
精彩评论