AngularJS Essentials
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Unit Testing<br />
The topics that we'll cover in this chapter are:<br />
• The Jasmine testing framework<br />
• Testing <strong>AngularJS</strong> components<br />
• Mocking with $httpBackend<br />
• Running tests with Karma<br />
The Jasmine testing framework<br />
Jasmine is an open source testing framework for the JavaScript language developed<br />
by Pivotal Labs. Its syntax is pretty similar to one of the most famous testing<br />
frameworks, RSpec.<br />
Basically, you just need to download it from GitHub at http://jasmine.github.io<br />
and unzip it. Later in this chapter, you'll see how to use it with Karma, a runner that<br />
could offer us many interesting benefits.<br />
To clarify our first step with Jasmine, let's implement a factory function based on our<br />
parkingService example from Chapter 4, Dependency Injection and Services, and use a<br />
Revealing Module Pattern:<br />
parkingFactoryFunction.js<br />
var parkingFactoryFunction = function () {<br />
var _calculateTicket = function (car) {<br />
var departHour = new Date().getHours();<br />
var entranceHour = car.entrance.getHours();<br />
var parkingPeriod = departHour - entranceHour;<br />
var parkingPrice = parkingPeriod * 10;<br />
return {<br />
period: parkingPeriod,<br />
price: parkingPrice<br />
};<br />
};<br />
return {<br />
calculateTicket: _calculateTicket<br />
};<br />
};<br />
[ 122 ]<br />
www.it-ebooks.info