zoukankan      html  css  js  c++  java
  • FlexUnit单元测试(第三章FlexUnit事件断言)

    在说明问题之前,大家就看一个例子:

    import flexunit.framework.TestCase;

    import flexunit.framework.Assert;

    import flash.utils.Timer;

    import flash.events.TimerEvent;

        public class TimerTest extends TestCase {

    private var _timerCount:int;

           public function TimerTest(methodName:String) {

                  super(methodName);

                  _timerCount = 0;

           }

        public function testTimer():void {

             var timer:Timer = new Timer(3000, 1);

             timer.addEventListener(TimerEvent.TIMER, incrementCount);

             timer.addEventListener(TimerEvent.TIMER_COMPLETE, verifyCount);

             timer.start();

        }

    private function incrementCount(timerEvent:TimerEvent):void {

            _timerCount++;

        }

    private function verifyCount(timerEvent:TimerEvent):void {

             Assert. assertEquals(1, _timerCount);

            }

        }

    我们要对testTimer()方法进行测试,程序很简单,当我们测试testTimer时,内里并没有抛出任何错误,因此我们看到的结果是正确的,但是当3秒过后,程序会自动执行verifyCount( )函数,这函数体里有assertEquals(1,_timerCount) 这个语句,显然,我们在incrementCount( )方法里把_timerCount增加了,因为timer只执行一次,所以_timerCount的值应为1,那么我们比较的结果应该是正确的。但如果我们这样写呢:

    Assert. assertEquals(100, _timerCount);

    我们可以看到,TestRunnerBase里依然显示正确,这是为什么呢?因为这个比较是在3秒后才执行的,在测试testTimer( )的时间只需短短的十几毫秒,那时并没有抛出任何错误,当testTimer( )执行完毕,该testcase便会被清除,于是就认为测试通过。

    说到这里,大家就会开始想解决以上问题的方法。在FlexUnit中,使用了addAsync()这个函数来解决上述问题。下面我们介绍addAsync( )函数的用法:

    addAsync( )有四个参数:

    1、 触发的事件函数。

    2、 监听的时间,单位为毫秒。

    3、 事件函数的参数,类型为Object,默认为空。

    4、 失败所要调用的函数,如果在指定时间内没有触发事件,则断言失败。默认为空。

    另外,addAsync()方法的返回值是一个函数。

    我们把上面的例子改为:

    public class TimerTest extends TestCase {

    private var _timerCount:int;

           public function TimerTest(methodName:String) {

                  super(methodName);

                  _timerCount = 0;

           }

        public function testTimer():void {

             var timer:Timer = new Timer(3000, 1);

             timer.addEventListener(TimerEvent.TIMER, incrementCount);

             timer.addEventListener(TimerEvent.TIMER_COMPLETE, addAsync(verifyCount , 3500) );

             timer.start();

        }

    private function incrementCount(timerEvent:TimerEvent):void {

            _timerCount++;

        }

    private function verifyCount(timerEvent:TimerEvent):void {

             Assert. assertEquals(15, _timerCount);

            }

    }

    这里,我们运行的时候,会发现程序在等待:

    3秒之后就会出现如下结果:

    这里有一点要注意,如果我们这样写:

    public function testTimer():void {

                         addAsync(verifyCount, 5000);

                         _timer = new Timer(3000, 1);

                         _timer.addEventListener(TimerEvent.TIMER, verifyCount );

                         _timer.start();

    }

    或者这样写:

    public function testTimer():void {

                                var function:Function = verifyCount;

                         addAsync(function, 5000);

                         _timer = new Timer(3000, 1);

                         _timer.addEventListener(TimerEvent.TIMER, function);

                         _timer.start();

    }

    都会提示为:“Asynchronous function did not fire after 5000 ms”这样的错误。这是为什么呢?明明在5秒的时间内verifyCount函数被执行了。

    但是当然们改成这样时:

    public function testTimer():void {

                         var function:Function = addAsync(verifyCount, 5000);

                         _timer = new Timer(3000, 1);

                         _timer.addEventListener(TimerEvent.TIMER, function);

                         _timer.start();

    }

    结果就正确了,那么大家就可以猜到,我们使用addAsync(verifyCount, 5000) 方法,并不是断言verifyCount是否被执行了,而是断言addAsync()方法所返回的函数是否被执行了,如果有执行,我们就调用verifyCount方法,如果没有就断言失败。

    另外要提一下的是,TestCase里还有setUp( )tearDown( )两个函数。setUp 方法将在每个测试方法之前运行,用于搭建通用的初始设置。tearDown 方法将在每个测试方法之后运行,用于进行通用的卸载或清除工作。 setUp  tearDown 方法是该 TestCase 对象中的每个测试方法运行一次,而非对这个测试用例运行一次。

  • 相关阅读:
    Luogu-P2295 MICE
    Luogu-P2627 修剪草坪
    Loj-10176-最大连续和
    Luogu-P1886 滑动窗口
    Luogu-P3807 【模板】卢卡斯定理
    Luogu-P1879 [USACO06NOV]玉米田Corn Fields
    Luogu-P1896 [SCOI2005]互不侵犯
    Loj-SGU 223-国王
    Luogu-P2657 [SCOI2009]windy数
    素数
  • 原文地址:https://www.cnblogs.com/fxair/p/1710741.html
Copyright © 2011-2022 走看看