06.07.2017 Views

Mastering JavaScript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Testing and Debugging<br />

This is the Jasmine report that shows the details about the number of tests that were<br />

executed and the count of failures and successes. Now, let's make the test case fail.<br />

We want to test a case where an undefined variable is passed to the function. Add<br />

one more test case as follows:<br />

it("can handle undefined", function() {<br />

var str= undefined;<br />

expect(capitalizeName(str)).toEqual(undefined);<br />

});<br />

Now, when you run SpecRunner.html, you will see the following result:<br />

As you can see, the failure is displayed for this test case in a detailed error stack.<br />

Now, we go about fixing this. In your original <strong>JavaScript</strong> code, we can handle an<br />

undefined condition as follows:<br />

function capitalizeName(name){<br />

if(name){<br />

return name.toUpperCase();<br />

}<br />

}<br />

With this change, your test case will pass and you will see the following in the<br />

Jasmine report:<br />

[ 150 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!