This weekend I began tinkering around with FlexUnit 2.0 for Actionscript 3.0. So far I am excited about the framework. It doesn’t do much, but it does the basics. When writing some of my tests I found I needed to compare two objects together. I know how to handle this in Java you would define the method equals(Object o) on the Customer object. I searched for a few hours and could find no such solution in Actionscript. Ofcourse I could make it my own standard to define this method but then frameworks like FlexUnit wouldn’t know anything about it.
Finally after searching I came across mx.utils.ObjectUtil. This object has numerous methods on it that expose the innards of objects. It has an especially powerful compare method that will transverse an object and compare each sub element testing for equality. This eliminates the need to write most equals methods on objects, but unfortunately it doesn’t get automatically used when you use the equality (==) operator.
This confuses me because the docs for equality (==) say it actually compares the value of the operand and strict equality (===) compares the memory references. Unfortunately this doesn’t seem to apply to objects as == and === have the same outcome in all my tests.
Although I am a little dissappoitned I can’t write something like:
object1 == object2
I have to instead write:
import mx.utils.ObjectUtil; ObjectUtil.compare(object1, object2) == 0
I wouldn’t mind as much if FlexUnit took this into account with there assertEquals method. Currently I believe it just runs an equality (==) operation and is done. So my tests have to use the assertTrue.
Anyways I wanted to write this post to do one of two things.
- Save someone the trouble of searching for hours to be as dissappointed as me
- Have someone point out my flaws and show me the easy great Actionscript way to do equality