views:

354

answers:

3

I'm looking for a reliable mocking framework for ActionScript. I've been using mock-as3, but I'm annoyed with what I feel is a hack-ish solution for triggering events. There are other a few other reasons why I'd like to have some options, but not sure if I necessarily need to go into them. I've also looked into Mock4AS, but the interface appears to be cumbersome. Any finds will be appreciated, thanks.

A: 

hi,

no, there is not really any such thing (that i know of) ... mock-as3 is the best you can get, in my opinion ... there is an open issue on adobe jira, that will hopefully be addressed some day, that would solve a lot of problems ... enabling proper mocking would be just one advantage ...

back2dos
+4  A: 

asmock is a dynamic mocking framework and supports triggering events as a response to a method being called. Despite still having the "beta" monikor, it has been used on several production applications (including inside a continuous integration server).

You would use it for your purpose like so:

var mock : ISometInterface = ISometInterface(mockFactory.createStrict(ISometInterface));
SetupResult.forEventDispatcher(mock); // stubs the IEventDispatcher methods
SetupResult.forCall(mock.someMethod())
           .dispatchEvent(new Event()); // dispatch an event when someMethod is called
mockFactory.replayAll();

mock.someMethod(); // will dispatch the event

If you have any issues working with asmock (or want to do something in particular), just shoot me a mail via sourceforge or put up a bug request.

A new project, based on the dynamic bytecode generation I did for asmock, has popped up called mockito (a port from the java framework).

Richard Szalay
Thank you Richard. I am starting a greenfield project and am going to give ASMock a shot.
t3hh00d
No worries. Please feel free to provide feedback through sourceforge if any of the documentation / tutorials are insufficient.
Richard Szalay
I can vouch for ASMock. I use it for every project i unit test now. It's extremely stable and feature rich.
James Hay
I second James : So far ASMock has proven quite mature, and quite well thought out.
Axelle Ziegler
FYI, I am aware of integration issues withh the FlexUnit 4 beta. It should be resolved in an upcoming release.
Richard Szalay
+1 for FlexUnit4 integration that works.
Stiggler
@Stiggler Thanks, please feel free to provide any feedback you have via the website :)
Richard Szalay
A: 

I'd be interested to hear why you think the Mock4AS interface is cumbersome. Perhaps we could improve it. Often you only need to use a few of the calls: record() in your mock. expects() in your test, withArgs() or withArg() perhaps. And then an assertTrue(mock.success());

We have provided a few more to give extra features like willReturn() and willThrow().

Also, I am about to push a change so that you can also set deepCompare() to compare object/array contents instead of just comparing by reference.

For dispatching events I created a MockEventDispatcher. I simply make it dispatch the events I want: mock.dispatchEvent(someEvent);

Any feedback would be appreciated.

Thanks.

-JP

jp