Asserting Results - J Query

Essential elements of any unit test are assertions. The author of the test needs to express the results expected and have the unit testing framework compare them to the actual values that an implementation produces.


QUnit provides three assertions.

ok(boolean[, message ])
The most basic one is ok(), which requires just one Boolean argument. When the argument is true, the assertion passes; otherwise, it fails. In addition, it accepts a string to display as a message in the test results:

The equals assertion uses the simple comparison operator (==) to compare the actual and expected arguments. When they are equal, the assertion passes; otherwise, it fails. When it fails, both actual and expected values are displayed in the test result, in addition to a given message:

Compared to ok(), equals() makes it much easier to debug tests that failed, because it’s obvious which value caused the test to fail.

The same() assertion can be used just like equals() and is a better choice in most cases. Instead of the simple comparison operator (==), it uses the more accurate comparison operator (===). That way, undefined doesn’t equal null, 0, or the empty string (""). It also compares the content of objects so that {key: value} is equal to {key: value}, even when comparing two objects with distinct identities. same() also handles NaN, dates, regular expressions, arrays, and functions, while equals() would just check the object identity:

In case you want to explicitly not compare the content of two values, equals() can still be used. In general, same() is the better choice.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

J Query Topics