fake chocolate, mock objects and test spies for AS3
This tagline is probably a hint that Mockolate is most useful when testing software. Whether you are doing test-driven-development, post-crunch-time fill-in-the-gaps, or exploratory-I-have-no-idea-what-is-going-on testing Mockolate can help.
A mock object can be used to simulate the behaviour of complex, real (non-mock) objects when using the real object would be impractical or impossible. Situations where a mock object would be useful:
- When an object is slow (like a database or webservice),
- is non-deterministic (like the current time),
- has states that are difficult to reproduce (like network connections)
The above is mostly appropriated from Mock Objects at Wikipedia. I could keep rewriting it here, but it’s really quite a good read.
In espionage, spies infiltrate a system, recording and relaying information to their handlers. The handlers may use that information to check facts, inform others, or take action.
In testing, a Test Spy records which methods are called, which getters are got, which setters are set. The handler (typically a testcase) can then check the facts against what should or should not have happened and take action (typically an assertion).
- choice of expectation-based or refactoring-friendly record-replay syntaxes
- helpful guiding API provided by fluent interfaces
- provides a FlexUnit 4 Rule and Runner
- uses proven libraries, FlexUnit 4, FLoxy and Hamcrest-AS3
Whats the code look like?
var fileReference:FileReference = nice(FileReference); // note using Hamcrest array() Matcher mock(fileReference).method("browse").args(array(FileFilter)) .dispatches(new Event(Event.SELECT)); // note expecting the default parameter values mock(fileReference).method("upload").args(URLRequest, "uploadData", false) .dispatches(new Event(Event.OPEN)) .dispatches(new ProgressEvent(ProgressEvent.PROGRESS, false, false, 23, 123), 10) .dispatches(new ProgressEvent(ProgressEvent.PROGRESS, false, false, 104, 123), 20) .dispatches(new DataEvent(DataEvent.UPLOAD_COMPLETE_DATA), 30) .dispatches(new DataEvent(Event.COMPLETE), 40); // use FileReference in the Test