Simulating as3 dispatch events for unit testing

224 Views Asked by At

I'm writing some unit test and would like to test how my components are reacting to some specific flash events. So I need a way to dispatch those events. I think that that's possible.

I'm trying to use asmock but trying to piece together info from http://asmock.sourceforge.net/wiki/Quick_Start_Guide is failing me.

If someone could point me to a complete example, that would be great! Also open to use other testing frameworks.

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

Hm, how about that:

public class  EventSimulator extends EventDispatcher
{
    public function doMouseClick():void {
        dispatchEvent(new MouseEvent(MouseEvent.CLICK));
    }

    public function doChange():void {
        dispatchEvent(new Event(Event.CHANGE));
    }
    //and so on..
}
1
On

If you're already using asMock, then you can do this:

var dispatcher2 : IEventDispatcher = 
 IEventDispatcher(mockRepository.createStub(IEventDispatcher, StubOptions.NONE));
mockRepository.stubEvents(dispatcher2);

// Call mockRepository.dispatchEvent() or .addEventListener() as normal

However, if you're not, then asMock would be overkill as per ZuzEL's answer.

(Disclaimer: I'm the author of asMock, and that documentation)