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

posted @ 2010-04-13 08:18  fxair  阅读(260)  评论(0编辑  收藏  举报