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.
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 ...
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).
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