fake chocolate,
mock objects and
test spies for AS3

Recording and Replaying

How to make Mockolates do something (alternate syntax)

In stubbing and mocking the default expectation-based syntax was demonstrated, however Mockolate also supports a record-replay syntax for refactoring-friendly Mock Objects. To start using this syntax pass a Mockolate-created instance to record(instance), call any methods, getters and setters that are expected and then call replay(instance) before passing the instance to whatever is going to use the it. Some expectations will use expect(...) to set additional behaviour.

Using a nice Flavour,

var flavour:Flavour = nice(Flavour);

set it to record,


we can stub a getter,


or a setter,

expect( = true);

or a method with no arguments, and return a value,


stub a method with arguments, and return a reference,

var otherFlavour:Flavour = nice(Flavour);
var combinedFlavour:Flavour = nice(Flavour);

stub a method with arguments using Matchers from hamcrest-as3


stub a setter with arguments using Matchers,

expect(flavour.ingredients = expectArg(array("cookies", "cream")));

stub a method to dispatch an Event,

    .dispatches(new FlavourEvent(FlavourEvent.TASTE_EXPLOSION));

stub a method to call a Function,

    .calls(function():void {
        trace("it's a mystery flavour");

stub a method to throw an Error,

    .throws(new ArgumentError("Really, combine this flavour with <null>?"));

and finally before using the instance for real, call replay()


Customising behaviour

Additional behaviour not included in Mockolate can be added by implementing mockolate.ingredients.answers.Answer. Examples of custom Answers are provided for handing HTTPService result and fault calls. Decorating Mocolates has more details.

next verifying and test spies