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 />
car.entrance = new Date(1401620400000);<br />
car.depart = new Date(1401649200000);<br />
var parkingService = parkingFactoryFunction();<br />
var ticket = parkingService.calculateTicket(car);<br />
expect(ticket.period).toBe(8);<br />
expect(ticket.price).toBe(80);<br />
});<br />
});<br />
In order to execute our specification, let's check out Jasmine's built-in HTML-based<br />
runner called SpecRunner.html, which can be configured as follows:<br />
SpecRunner.html<br />
<br />
<br />
<br />
Jasmine Spec Runner v2.0.0<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
After including the parkingFactoryFunction.js and<br />
parkingFactoryFunctionSpec.js files, we just need to open the<br />
SpecRunner.html file in our browser.<br />
Testing <strong>AngularJS</strong> components<br />
To test any <strong>AngularJS</strong> component such as controllers, directives, filters, and<br />
services, we need to go beyond the basics and use the ngMock library. It provides a<br />
dependency injection mechanism, allowing us to locate and inject any component of<br />
a specified module. Also, there are services such as $http, $log, and $timeout that<br />
could be mocked in order to allow our code to be more testable.<br />
[ 124 ]<br />
www.it-ebooks.info