Skip to content

Instantly share code, notes, and snippets.

@drewbourne
Created October 19, 2010 05:31
Show Gist options
  • Save drewbourne/633667 to your computer and use it in GitHub Desktop.
Save drewbourne/633667 to your computer and use it in GitHub Desktop.
public class EventRuleExamples
{
[Rule]
public var expectEvent:EventRule = new EventRule();
[Rule]
public var expectOrderedEvent:EventRule = new EventRule(/*ordered = */ true);
public var dispatcher:IEventDispatcher;
[Before]
public function setup():void
{
dispatcher = new EventDispatcher();
}
[Test]
public function exampleExpectEvent():void
{
expectEvent.from(dispatcher).withType(Event.COMPLETE).once();
}
[Test]
public function exampleExpectEventWithProperties():void
{
expectEvent.from(dispatcher).withType(EntityEvent.CREATE)
.instanceOf(EntityEvent)
.hasProperties({ entity: instanceOf(Entity) })
.once();
setTimeout(function():void {
dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
}, 50);
}
[Test]
public function exampleExpectEventWithTimeout():void
{
expectEvent.from(dispatcher).withType(EntityEvent.CREATE)
.instanceOf(EntityEvent).once()
.timeout(500, function():void {
// do timeout assertions
});
setTimeout(function():void {
dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
}, 100);
}
[Test]
public function exampleExpectEventWithProceed():void
{
expectEvent.from(dispatcher).withType(EntityEvent.CREATE)
.instanceOf(EntityEvent).once()
.proceedOnEvent();
setTimeout(function():void {
dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
}, 100);
}
[Test]
public function exampleExpectEventWithEventListener():void
{
expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
.calls(function(event:Event):void {
// do event assertions
});
setTimeout(function():void {
dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
}, 100);
}
[Test]
public function exampleExpectEventWithSuccess():void
{
expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
.onEventProceed()
.onEventWait()
.onEventCall();
.then.proceed()
.then.wait(500)
.then.calls(function():void {
// do next step
});
expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
.calls(expectEvent.proceed);
expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
.proceed();
setTimeout(function():void {
dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
}, 100);
}
[Test]
public function exampleExpectEventWithWaits():void
{
expectEvent.from(dispatcher).withType(EntityEvent.CREATE).once()
.waitFor(EntityEvent.CREATE_SUCESS));
expectEvent.from(dispatcher).withType(EntityEvent.CREATE_SUCESS).once()
.proceed()
dispatcher.dispatchEvent(new EntityEvent(new Entity(), EntityEvent.CREATE));
}
[Test]
public function expectEventWithSequence():void
{
expectOrderedEvent.from(dispatcher).withType("create");
expectOrderedEvent.from(dispatcher).withType("createSuccess");
expectOrderedEvent.from(dispatcher).withType("destroy");
expectOrderedEvent.from(dispatcher).withType("destroySuccess").proceed();
}
[Test]
public function expectsManyEvents():void
{
var collection:ArrayCollection = new ArrayCollection();
expectEvent.from(collection).withType(CollectionEvent.COLLECTION_CHANGE)
.hasProperties({ kind: CollectionEventKind.ADD }).atLeast(3)
.proceed();
collection.addItem({ id: 1 });
collection.addItem({ id: 2 });
collection.addItem({ id: 3 });
// what happens? success or failure?
collection.addItem({ id: 4 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment