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.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值