06.07.2017 Views

Mastering JavaScript

Create successful ePaper yourself

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

Chapter 6<br />

<br />

<br />

<br />

<br />

The first three are Jasmine's own framework files. The next section includes the<br />

source files that we want to test and the actual test specifications.<br />

Let's experiment with Jasmine with a very ordinary example. Create a<br />

bigfatjavascriptcode.js file and place it in the src/ directory. We will test the<br />

following function:<br />

function capitalizeName(name){<br />

return name.toUpperCase();<br />

}<br />

This is a simple function that does one single thing. It receives a string and returns a<br />

capitalized string. We will test various scenarios around this function. This is the unit<br />

of code that we discussed earlier.<br />

Next, create the test specifications. Create one <strong>JavaScript</strong> file, test.spec.js, and<br />

place it in the spec/ directory. The file should contain the following. You will need<br />

to add the following two lines to SpecRunner.html:<br />

<br />

<br />

The order of this inclusion does not matter. When we run SpecRunner.html, you<br />

will see something as follows:<br />

[ 149 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!