PureMVC的FlexUnit单元测试

 

The PureMVC FlexUnit Testing project is already quite old (last update was June 2008), but I just discovered it because I am currently working on a big Adobe AIR project that currently does not have a User Interface yet. We use the PureMVC framework for most of our bigger applications, and because it depends on Notifications it's not that easy to Unit Test. Proxies don't return results synchronously, but send notifications with the result in the body.

Well this small library helps connecting y our PureMVC implementation code to the FlexUnitframework. The following example code shows the code for a Test Class for a DataProxy that has a method getSomeData(). As you can see, you need to write some code to access the facade, proxy, ... but it's all pretty straight forward.

 

package unitTests  
{  
  import net.hufkens.example.ApplicationFacade;  
  import net.hufkens.example.config.ApplicationNotifications;  
  import net.hufkens.example.model.DataProxy;  
  import com.andculture.puremvcflexunittesting.*;  
  import org.puremvc.as3.core.View;  
  import org.puremvc.as3.interfaces.IView;  
  public class DataProxyTest extends PureMVCTestCase  
  {  
    private var timeout:int = 0;  
    public function DataProxyTest(methodName:String=null)  
    {  
      super(methodName);  
    }  
    protected function get facade():ApplicationFacade  
    {  
      return ApplicationFacade.getInstance();  
    }  
    public override function setUp():void  
    {  
      facade.registerProxy(new DataProxy());  
    }  
    protected function get proxy():DataProxy  
    {  
      var proxy:DataProxy =  
      DataProxy(facade.retrieveProxy(DataProxy.NAME));  
      return proxy;  
    }  
    protected function get view():IView  
    {  
      return View.getInstance() as IView;  
    }  
    public function testGetSomeData():void  
    {  
      registerObserver(view, proxy,  
        ApplicationNotifications.GET_SOME_DATA_DONE,  
        response, timeout);  
      proxy.getSomeData();  
    }  
    private function response(e:PureMVCNotificationEvent):void  
    {  
      assertTrue("data is not available",  
      e.notification.getBody().data.length>0);  
    }  
  }  
}  

 30JUL/098

 

Testing PureMVC code with FlexUnit

The PureMVC FlexUnit Testing project is already quite old (last update was June 2008), but I just discovered it because I am currently working on a big Adobe AIR project that currently does not have a User Interface yet. We use the PureMVC framework for most of our bigger applications, and because it depends on Notifications it's not that easy to Unit Test. Proxies don't return results synchronously, but send notifications with the result in the body.

Well this small library helps connecting your PureMVC implementation code to the FlexUnitframework. The following example code shows the code for a Test Class for a DataProxy that has a method getSomeData(). As you can see, you need to write some code to access the facade, proxy, ... but it's all pretty straight forward.

package unitTests
{
  import net.hufkens.example.ApplicationFacade;
  import net.hufkens.example.config.ApplicationNotifications;
  import net.hufkens.example.model.DataProxy;

  import com.andculture.puremvcflexunittesting.*;

  import org.puremvc.as3.core.View;
  import org.puremvc.as3.interfaces.IView;

  public class DataProxyTest extends PureMVCTestCase
  {
    private var timeout:int = 0;

    public function DataProxyTest(methodName:String=null)
    {
      super(methodName);
    }

    protected function get facade():ApplicationFacade
    {
      return ApplicationFacade.getInstance();
    }

    public override function setUp():void
    {
      facade.registerProxy(new DataProxy());
    }

    protected function get proxy():DataProxy
    {
      var proxy:DataProxy =
      DataProxy(facade.retrieveProxy(DataProxy.NAME));
      return proxy;
    }

    protected function get view():IView
    {
      return View.getInstance() as IView;
    }

    public function testGetSomeData():void
    {
      registerObserver(view, proxy,
        ApplicationNotifications.GET_SOME_DATA_DONE,
        response, timeout);

      proxy.getSomeData();
    }

    private function response(e:PureMVCNotificationEvent):void
    {
      assertTrue("data is not available",
      e.notification.getBody().data.length>0);
    }
  }
}
  It worked great for this project. I hope it will still be supported for Flex 4 so we can use the built-in unit testing features of Flash Builder.

Make sure you checkout the latest version from Google Code, because the swc file has an issue with multicore namespaces. The latest version in svn works fine.

 

你可能感兴趣的:(.net,Flex,单元测试,Adobe,AIR)