Conditional Compilation to Mock with Swiz

The Flex compiler supports conditional compilation and now I had the first use-case for it.

I am still playing around with the Swiz framework and I wanted to have an easy way to switch between mocked and real data. For that reason I added the two compile constants mock and release in the CONFIG namespace:

compiler settings

To support mock and real data I have two different BeanLoaders, one with the real service and one with the mock service. In my main mxml I have the following preinitialize event listener:

private function onPreInit():void
	var serviceBeanLoader:Class;
		serviceBeanLoader = ServiceMockBeanLoader;
		serviceBeanLoader = ServiceBeanLoader;
	Swiz.loadBeans([GeneralBeanLoader, serviceBeanLoader]);

The GeneralBeanLoader contains models and controllers which are independent from a server implementation and the ServiceBeanLoader contains server specific stuff like for instance DynamicChannelSet, HTTPService, RemoteObject, DataService etc.. The ServiceMockBeanLoader contains the same ids but mocked implementations. If you are using Delegates it can be even simpler because you can easily mock an AsyncToken like this:

private var mockAsyncToken:AsyncToken;
public function loadSomeData():AsyncToken
	mockAsyncToken = new AsyncToken(null);
	// wait 500ms before invoking the result
	var t:Timer = new Timer(500, 1);
	t.addEventListener(TimerEvent.TIMER_COMPLETE, onTimer);
	return mockAsyncToken;
private function onTimer(event:TimerEvent):void
	Timer(event.currentTarget).removeEventListener(TimerEvent.TIMER_COMPLETE, onTimer);
	for each(var responder:IResponder in mockAsyncToken.responders)
		responder.result(new ResultEvent(ResultEvent.RESULT, false, true, RESULT));
private static const RESULT:XML = <result>could be the mocked result</result>;

What are your best practices to use mock objects?

5 comments to Conditional Compilation to Mock with Swiz

  • […] Otro aspecto no resuelto, por el momento, es el tema de los mock objects, pero seguro que hay alguna solución si se estudia detenidamente[3] […]

  • Thanks for the example.

    I can’t see where GeneralBeanLoader is being defined in your code though? Or is this declared in an external class? If you can elaborate a bit that would be appreciated.

  • hi jason,
    the GenralBeanLoader would load service independent things like model and controller.
    check my example I just posted. There is a download link at the end and you could have a look at the complete source:

  • So that latest example isn’t using the conditional compilation technique you’ve described in this post? Instead you’ve basically moved what you’ve got in the onPreInit() call in this post into the Beans.mxml file of the latest example and commented out the live server business component for development purposes…?

  • No, the new example shows how to use a mocked delegate.
    But you can easily change the example to use conditional compilation.
    Split up the Beans.mxml into three different BeanLoaders like GeneralBeans.mxml, ServiceBeans.mxml and ServiceMockBeans.mxml.
    In the ServiceMockBeans.mxml you put the UserMockDelegate and in the ServiceBeans you put the UserDelegateImpl. The rest ends up in the GeneralBeans BeanLoader class.
    Then you would load the beans like in this example with conditional compilation.