Home > Flex Gotchas > assertEquals using != and the difference between != and !==

assertEquals using != and the difference between != and !==

When using the assertEquals method in Flexunit 4, it is important to understand how the != operator works.

I was under the assumption that != checked the values of the items on either side of the operand, while the !== checked the reference, but != checking values is only partially true.

The full details can be found here, but interestingly, the last bullet item states:

Variables representing objects, arrays, and functions are compared by reference. Two such variables are equal if they refer to the same object, array, or function. Two separate arrays are never considered equal, even if they have the same number of elements.

Wow, that’s kinda lame. I thought !== was supposed to check by reference? Regardless of the differences (you can find !== details here) and debate between what the two operands should do, the message I was getting when my assertEquals failed on two objects showed the same string message.

To add to my confusion, it just so happened that the two static classes I was comparing in the assertEquals method had toString() methods. When the asserEquals failed, the toString() method was called on each and returned the same value and I was getting something like

expected: <My String> but was: <My String>

If I didn’t have the toString method, I would have received the generic

expected: <object Type> but was: <object Type>

The bottom line when using the asserEquals method is to create some sort of comparison value and assert that those are equal. In my case, I changed assertEqual(obj1, obj2) to assertEqual(obj1.toString(), obj2.toString()). Maybe toString() can be used as a fallback inside of assertEquals since that would be used for the failure output anyways? I don’t know what the ramifications of that would be, and I really don’t want to think about it since asserting that toString() returns the same thing from both objects is acceptable in my case.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: