drewbourne / mockolate

fake chocolate, mock objects and test spies for AS3
http://mockolate.org/
MIT License
145 stars 27 forks source link

Event dispatches twice #31

Closed andreymir closed 13 years ago

andreymir commented 13 years ago

Trying to mock ITransmitter interface, wich is derived from IEventDispatcher and dispatch event. The problem is that event dispatches twice. Here is the example:

var events: Array = new Array();
var t: IEventDispatcher = nice(ITransmitter);
stub(t).asEventDispatcher();
t.addEventListener(UploadEvent.COMPLETE, function(e: UploadEvent): void {
    events.push(e);
});
t.dispatchEvent(new UploadEvent(UploadEvent.COMPLETE));

At the end, 'events' array contains 2 objects, but should only one.

andreymir commented 13 years ago

I'm using 0.9.5 version

andreymir commented 13 years ago

Ooops, don't want to close the issue ... can not reopen :(( Sorry. Should I create new issue?

andreymir commented 13 years ago

As a workaround use this function instead of asEventDispatcher() method: private function setupDspatcher(o: IEventDispatcher): void { var dispatcher: EventDispatcher = new EventDispatcher(o); stub(o).method('addEventListener').callsWithArguments( function(type: String, listener: Function, useCapture: Boolean = false, priority: int = 0, useWeakReference: Boolean = false): void { dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); } ); stub(o).method('removeEventListener').callsWithArguments( function(type: String, listener: Function, useCapture: Boolean = false): void { dispatcher.removeEventListener(type, listener, useCapture); } ); stub(o).method('dispatchEvent').callsWithArguments(function(e: Event): Boolean { return dispatcher.dispatchEvent(e); }); }

drewbourne commented 13 years ago

I've opened the issue, will check it out. The asEventDispatcher() should do much the same your setupDispatcher() function.

drewbourne commented 13 years ago

Confirmed. Fixing now, I'll reply again once I've pushed the fix.

drewbourne commented 13 years ago

fixed.

andreymir commented 13 years ago

Thanks! Works now.