drewbourne / mockolate

fake chocolate, mock objects and test spies for AS3
http://mockolate.org/
MIT License
145 stars 27 forks source link

Allow for creation of mock DisplayObject from interfaces #55

Open weltraumpirat opened 12 years ago

weltraumpirat commented 12 years ago

When creating mock objects from an interface, all of its super classes are included in the mock objects type hierarchy. But interfaces in ActionScript can extend only other interfaces, and since DisplayObject, Sprite and MovieClip don't implement one, we can't type cast our mock objects to any of these. Hence, we lose the ability to use these mock objects as an argument in calls to addChild () or removeChild() - which basically prevents the fully decoupled testing of UI elements.

If a subclass of DisplayObject implements a custom interface, it should be possible to create a mock object, which extends from DisplayObject or one of its descendants, instead of just plain Object.

I would imagine a syntax like this:

nice (IUISubClass).asSprite();
nice (IUISubClass).asMovieClip();

or via metadata:

[Mock (base="flash.display.Sprite")]
public var uiSubClass:IUISubClass;

The same approach could of course also be useful for any other classes that need to be used in type casts to classes that don't implement an interface.

drewbourne commented 12 years ago

Excellent idea. The superclass to extend will need to be specified before preparing the proxy classes though.

When preparing:

prepare({ "class": IUISubClass, "extend": Sprite });

Via Metadata:

[Mock (extend="flash.display.Sprite")]
public var uiSubClass:IUISubClass;
weltraumpirat commented 12 years ago

Moving the declaration into the preparation is much cleaner. And "extend" is also a better keyword. Sweet. :)

weltraumpirat commented 12 years ago

Workaround for now:

Create a skeleton class (empty method trunks) that extends Sprite (or MovieClip, or...) and implements the interface. Then use this class to create a mock object.

drewbourne commented 12 years ago

To take this a step further it should allow specifying multiple interfaces:

prepare({ 'class': Sprite, 'implement': [ IButton, ISelectable ] })
[Mock(implement="example.IButton, example.ISelectable")]
public var component:Sprite;
weltraumpirat commented 12 years ago

Good point! Until then, the same workaround as above applies.