Book Image

JavaScript Unit Testing

By : Hazem Saleh
Book Image

JavaScript Unit Testing

By: Hazem Saleh

Overview of this book

<p>The largest challenge for many developers’ day to day life is ensuring the support of, and assuring the reach of, their product. With the ever increasing number of mainstream web browsers this is becoming a more difficult task for JavaScript coders. <br /><br />From the beginning, JavaScript Unit Testing will show you how to reduce the time you spend testing, and automate and ensure efficiency in guaranteeing your success.<br /><br />JavaScript Unit Testing will introduce and help you master the art of efficiently performing and automating JavaScript Unit tests for your web applications.<br /><br />Using the most popular JavaScript unit testing frameworks, you will develop, integrate, and automate all the tests you need to ensure the widest reach and success of your web application.<br /><br />Covering the most popular JavaScript Unit testing frameworks of today, JavaScript Unit Testing is your bible to ensuring the functionality and success of all of your JavaScript and Ajax Web Applications.<br /><br />Starting with Jasmine, you will also learn about, and use, YUITest, QUnit, and JsTestDriver, integrate them into your projects, and use them together to generate reports.<br /><br />Learn to automate these tools, make them work for you, and include the power of these tools in your projects from day one.</p>
Table of Contents (12 chapters)

Assertions


An assertion is a function that validates a condition if the condition is not valid; it throws an error that causes the test to fail. A test method can include one or more assertions; all the assertions have to pass in order that the test method passes. In the first YUI test example, we used the Y.Assert.areEqual assertion. In this section, the other different built-in assertions provided by YUI Test will be illustrated.

The assert assertion

The assert function takes two parameters. The first parameter is a condition, and the second parameter represents a failure message. It is passed if the condition is true, and when it fails, the failure message is displayed. For example:

Y.assert(10 == 10, "Error ..."); // will pass
Y.assert(10 != 10, "Error ..."); // will fail and display an error

The areEqual and areNotEqual assertions

The areEqual assertion function takes three parameters; the first two parameters represent the expected and actual values, and the third parameter is optional...