Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Unit Test advice needed  (Read 3230 times)
olist
Newbie
*
Posts: 7


View Profile Email
« 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
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 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>
Logged
olist
Newbie
*
Posts: 7


View Profile Email
« Reply #2 on: November 04, 2008, 07:24:51 »

Thanks, I'll go that direction!
Logged
Pages: [1]
Print