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.
Chapter 7<br />
Before writing the test, you should take care about the existing dependencies in<br />
your code. In this case, inside the _calculateTicket function, we are creating a<br />
Date object by calling the new operator. This kind of a situation should be avoided,<br />
otherwise you can't write an effective test.<br />
The following code considers the depart property inside the parked car object. In<br />
this way, we could manage the depart property, and thus be able to test it properly:<br />
parkingFactoryFunction.js<br />
var parkingFactoryFunction = function () {<br />
var _calculateTicket = function (car) {<br />
var departHour = car.depart.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 />
The creation of the parkingFactoryFunctionSpec function starts by calling the<br />
describe function. It takes a description of the specification and a function that<br />
contains the test scenarios:<br />
parkingFactoryFunctionSpec.js<br />
describe("Parking Factory Function Specification", function () {<br />
});<br />
Now, we need to create each of our test scenarios through the it function. Here, we<br />
will need to place a description and expectation for each test:<br />
parkingFactoryFunctionSpec.js<br />
describe("Parking Factory Function Specification", function () {<br />
it("Should calculate the ticket for a car that arrives any day at<br />
08:00 and departs in the same day at 16:00", function () {<br />
var car = {place: "AAA9988", color: "Blue"};<br />
[ 123 ]<br />
www.it-ebooks.info