Skip to main content
Rupam_K
Known Participant
July 14, 2009
Answered

Error in assertEquals

  • July 14, 2009
  • 1 reply
  • 3766 views

Hi Everyone,

I tried a simple example with timer event and once it gets complete , i call a function to check my assert. There is a for loop which checks for all the assert, but i get an error in that loop if any of my assert is not true. Can anybody help out. My sample code i am pasting

this is my test method in my class.

public function testTimer2():void {
            _timer = new Timer(1000, 2);
            _timer.addEventListener(TimerEvent.TIMER, incrementCount, false, 0, true);
            _timer.addEventListener(TimerEvent.TIMER_COMPLETE, addAsync(verifyCount, 2500, 2), false, 0, true);
            _timer.start();
          
        }

this function is getting called twice

   private function incrementCount(timerEvent:TimerEvent):void {
            
             _timerCount++;
             trace("increment count");
         }

After the final timer event completes i am checking all my test cases in this function.

private function verifyCount(timerEvent:TimerEvent, expectedCount:int):void {
            trace("verify count");
            for(var i:int = 0 ; i<2; i++)
            {
            assertEquals("verfiy count",expectedCount, _timerCount);
            assertEquals("temp", 0 , 1);
             }
        }    

this function throws an error in the for loop for first time and dont execute the loop for the second time , but i dont know the reason. It says expected 0 but 1. I know my test result is not true but it throws an exception.

Thanks in advance

This topic has been closed for replies.
Correct answer mlabriola

Rupam,

As I said in the beginning of my last message:

"First, any failed assertion means a test is over. So, once you say assertEquals(), if those two things are not equal, the test is over, it doesn't matter if you are in a loop or not... the test is done. The code throws an assertion error which the framework then handles to mark your test as a failure."

It will *never* compare the second one if the first one fails... the test is over after the first incorrect assertion. That is how assertions work. This is not a bug, this is correct.

Mike

1 reply

Participating Frequently
July 14, 2009

Rupam,

There are a few problems here.

First, any failed assertion means a test is over. So, once you say assertEquals(), if those two things are not equal, the test is over, it doesn't matter if you are in a loop or not... the test is done. The code throws an assertion error which the framework then handles to mark your test as a failure.

Also, as your code shows (and your comments show) the code that increments your counter runs twice. Each time it runs, the state of your counter increments... but it can only ever be one value at any given time.

The code that checks to see what the current value is runs once, but has a loop that tries to check if your counter is equal to multiple different values. Since a variable can only ever hold one value at a time, there is no way that a loop that tries to ensure it is simultaneously equal to several different values is going to work. This code will always fail. You cannot test the results of all of your methods in a single handler as your comment indicates.

Finally, in your loop, you say assertEquals("temp", 0 , 1);... which basically says that you believe 0 is supposed to be equal to 1. So, the only time it would ever make it through this loop sucessfully is when 0 is equal to 1...

expected 0 but 1 is telling you that it was expecting to receive a 0, but the value it was actually passed was a 1.

I am not sure if I am answering your question completely, so please let me know if I am missing something else. I saw that you referred to it throwing an error and wanted to make sure I am not missing the point completely.

Mike

Rupam_K
Rupam_KAuthor
Known Participant
July 14, 2009

Hi mike,

Thanks for a quick reply,

Well i understood what u tried to say, that was just a sample application. Here take a look at new code.

I declare an array

public var resultArray:Array = new Array;

this is my timer function . In this timer function i am calling my incrementCount twice.

public function testTimer2():void {
            _timer = new Timer(1000, 2);
            _timer.addEventListener(TimerEvent.TIMER, incrementCount, false, 0, true);
            _timer.addEventListener(TimerEvent.TIMER_COMPLETE, addAsync(verifyCount, 5000), false, 0, true);
            _timer.start();
            trace("testTimer2");
        }

          In my incrementCount i am storing my result of count in an array called resultArray.

private function incrementCount(timerEvent:TimerEvent):void {
           
            _timerCount++;
            resultArray.push(_timerCount);
            trace("increment count");
        }


Once the timer event is complete , here I need to check all the values for test. But it throws me error , if any of my value is mismatch and dont check for rest of the value in the loop

private function verifyCount(timerEvent:TimerEvent):void {
            trace("verify count");
            for(var i:int = 0 ; i<2; i++)
            {
            assertEquals("verfiy count",i, resultArray);
         
             }
        }

In this function i know that my values stored in resultArray is [1] & [2] resply, and I am chcking it with [0] & [1], both shoudl fail, but it comes out of the loop in the very first time. It doesnt check for the second value.

I hope you got what i am trying to explain.

Thanks in advance.

Rupam

mlabriolaCorrect answer
Participating Frequently
July 14, 2009

Rupam,

As I said in the beginning of my last message:

"First, any failed assertion means a test is over. So, once you say assertEquals(), if those two things are not equal, the test is over, it doesn't matter if you are in a loop or not... the test is done. The code throws an assertion error which the framework then handles to mark your test as a failure."

It will *never* compare the second one if the first one fails... the test is over after the first incorrect assertion. That is how assertions work. This is not a bug, this is correct.

Mike