12.07.2015 Views

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 WRITING BETTER JAVASCRIPT};equal(utils.composeString(config), "value");});test("Null value", function() {var config ={prefix: "prefix",suffix: "suffix",value: null};equal(utils.composeString(config), "prefixsuffix");});test("No value property", function() {var config ={prefix: "prefix",suffix: "suffix",};equal(utils.composeString(config), "prefixsuffix");});});});AMD Teststest markup, will be hiddenEach test is defined with the test function, with arguments <strong>for</strong> the name of the test and a functionthat contains the test code. In each of the four tests I have added, I create an object with the prefix,suffix, and value properties that are passed to my function via my custom data bindings and pass thisto the composeString function, which I access through the utils argument to my requireJS callbackfunction, like this:equal(utils.composeString(config), "prefixvalue");Like most unit test packages, QUnit provides a series of assertions that test the result of anoperation. In this case, I have used the equal function to check that the result from calling thecomposeString function matches my expectation. A range of different assertions are available, and youcan see the full list at http://docs.jquery.com/QUnit.To run the unit tests, simply load tests.html into the browser. QUnit will per<strong>for</strong>m each test in turnand use the markup as a container <strong>for</strong> the results. My composeString function passes one of the tests andfails the other two. The results are displayed in the browser, as shown in Figure 9-2.255www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!