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 Tests for my pureMVC proxies with asyncronous data.  (Read 13205 times)
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« on: January 20, 2008, 01:55:51 »

I want to unit test my pureMVC application as much as possible, but I am having some problems conceptualizing the process.

I have several remote service data proxies. These contain various methods for other proxies, mediators, and commands to format their foreign key data ect. I'm trying to figure out how exactly to unit test this, because I feel like I should test off of 'generic' and/or arbitrary data instead of the live data from my server. When my service proxies are instantiated they fire off a remote method to fill the data property of the proxy with an array of data from the server.

So now I am sitting here staring at my FlexUnit class, and wondering how the heck I am supposed to do this. Perhaps this is too general without knowledge of my classes, or does this make sense? Any help would be greatly appreciated.

Cheers.

Joel
« Last Edit: January 20, 2008, 10:26:22 by Joel Hooks » Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #1 on: January 20, 2008, 01:58:52 »

hmm, upon further staring, I thought perhaps I should add an array parameter to my methods that is empty, so that if it isn't populated it is filled with the data property, otherwise it takes the input (from my test case) and uses that data instead.

?
Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #2 on: January 20, 2008, 02:13:07 »

Or, I move the service calls from the constructor and then simply pass the contrived data when I create the proxy instance in my test. This is probably cleaner. I think either way the service calls should be outside the constructor.
Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #3 on: January 20, 2008, 04:17:00 »

I'm currently only planning to unit test my Proxies, but I was curious if you'd also UT Mediators?
Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #4 on: January 20, 2008, 04:50:43 »

Testing with mock objects is the way you do this sort of thing:

Here's are a couple of good articles to get you started thinking about mock objects:
http://en.wikipedia.org/wiki/Mock_object
http://www.ociweb.com/jnb/jnbAug2005.html
http://www.ibm.com/developerworks/library/j-mocktest.html

What to mock is the service. Where you get the service so the Proxy can get the mock service or the real service is up to you.

In your Proxy, rather than instantiating the service in the constructor with a 'new HTTPService'  (or which ever component you're using), you could instead call a private 'createService()' method, which creates and sets the property that holds the service in the Proxy.

In Java, you can create a new instance and supply a method for override in the new statement. We can't in as3, so the problem becomes how to get that method overridden with one that returns a mock service. You could subclass the original proxy, overriding only that method, but you'd have to be sure your private methods and props were protected instead. Too invasive for my taste.

Here is a thread about an AS3 mock framework:
http://thefoundry.anywebcam.com/index.php/actionscript/mock-as3-released-a-mock-object-library-for-actionscript-3/

-=Cliff>
Logged
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #5 on: January 20, 2008, 10:20:31 »

you could instead call a private 'createService()' method

This is essentially what I did, but my command that initiates the proxies calls a 'retrieveData()' public method. So this way when I instantiate my proxy in my unit tests I can just pass my mock data to the proxy's constructor.

Workin good so far. This is the first time I've used unit tests. Read a lot about them, but never implemented. It is actually pretty cool, it has made me reshape my methods. Trying to be a good soldier. ;)

Thanks for the clues.
« Last Edit: January 20, 2008, 10:31:20 by Joel Hooks » Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
ohappyle
Newbie
*
Posts: 3


View Profile Email
« Reply #6 on: September 16, 2008, 12:03:47 »

...When my service proxies are instantiated they fire off a remote method to fill the data property of the proxy with an array of data from the server....

You can try this to get data from the server in your unit test:

         var timer:Timer = new Timer(1);
         
         //because the remote call will be asynchronous, then test it here
         var testSuccess:Function = function(evt:*= null):void {
            //for info on facade catching events
            //http://forums.puremvc.org/index.php?topic=276.0
            if (facade.wasReceived(AuthenticationProxy.LOGIN_SUCCESSFUL)) {
               trace("testing async stuff");
               timer.stop();
            }
            else {
               //test will loop until server return data
               timer.addEventListener(TimerEvent.TIMER, addAsync(testSuccess, 10000));
            }
            
         }
      
         
         timer.addEventListener(TimerEvent.TIMER, addAsync(testSuccess, 10000));         
         timer.start();
      

Logged
Pages: [1]
Print