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 对象中的每个测试方法运行一次,而非对这个测试用例运行一次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值