PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: olist on November 04, 2008, 05:55:46



Title: Unit Test advice needed
Post by: olist on November 04, 2008, 05:55:46
Hi all,
I'm using FlexUnit to unit test the proxies in my multicore PMVC app. In my test setup I use a TestFacade instead of the original ApplicationFacade since I don't need the whole bunch of Views and Mediators for the moment. Writing tests for simple proxies is no problem, but what is the best approach if I have proxies that know each other and I want to replace the proxy instance that is not under test with a mock object?

Consider this example: I have a UserProxy that knows the user's login state and a GameSetupProxy that I want to ask if a game can be started, which is the case when the user is logged in and other requirements are met. Sample code:
:
public class UserProxy extends Proxy {
public static const NAME : String = "UserProxy";

public function UserProxy() {
super(NAME);
}

public function isLoggedIn():Boolean {
// Perform some check here
return true;
}
}

public class GameSetupProxy extends Proxy {
public static const NAME : String = "GameSetupProxy";

public var requirementsMet : Boolean;

public function GameSetupProxy() {
super(NAME);
}

public function canStartGame() : Boolean {
var userProxy:UserProxy = UserProxy(facade.retrieveProxy(UserProxy.NAME));
return requirementsMet && userProxy.isLoggedIn();
}
}
Testing the UserProxy is easy, but when it comes to testing GameSetupProxy, I want to test the class in isolation and therefore created a UserProxy mock. Now the line
:
UserProxy(facade.retrieveProxy(UserProxy.NAME)); makes no sense anymore since I have registered the UserProxy mock with the facade instead of the original UserProxy.
I can refactor GameSetupProxy to be able to exchange the UserProxy instance, e.g. by letting both, mock and UserProxy implement the same interface, but maybe you can give me additional advice.

Thanks a lot, Olli


Title: Re: Unit Test advice needed
Post by: puremvc on November 04, 2008, 06:34:43
Olli,

Your on the right track with the interface. If the proxy and its mock implement the same interface, and you retrieve the proxy and cast it to the interface before use you'll be fine.

-=Cliff>


Title: Re: Unit Test advice needed
Post by: olist on November 04, 2008, 07:24:51
Thanks, I'll go that direction!