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 />
parkingService = _parkingService_;<br />
}));<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 />
car.entrance = new Date(1401620400000);<br />
car.depart = new Date(1401649200000);<br />
var ticket = parkingService.calculateTicket(car);<br />
expect(ticket.period).toBe(8);<br />
expect(ticket.price).toBe(80);<br />
});<br />
});<br />
Controllers<br />
The controller will be the next component that we will test. In the following code,<br />
there is the code of our controller from Chapter 2, Creating Reusable Components<br />
with Directives:<br />
parkingCtrl.js<br />
parking.controller("parkingCtrl", function ($scope) {<br />
$scope.appTitle = "[Packt] Parking";<br />
$scope.cars = [];<br />
$scope.colors = ["White", "Black", "Blue", "Red", "Silver"];<br />
$scope.park = function (car) {<br />
car.entrance = new Date();<br />
$scope.cars.push(car);<br />
delete $scope.car;<br />
};<br />
});<br />
It is more complex to test controllers than services because we need to mock its<br />
$scope. We will start by injecting the $controller dependency, which will be<br />
responsible for instantiating new controllers. Also, we need the $rootScope<br />
dependency in order to create a new $scope object through its $new function.<br />
[ 126 ]<br />
www.it-ebooks.info