Testing AngularJS components

To test any AngularJS component such as controllers, directives, filters, and services, we need to go beyond the basics and use the ngMock library. It provides a dependency injection mechanism, allowing us to locate and inject any component of a specified module. Also, there are services such as $http, $log, and $timeout that could be mocked in order to allow our code to be more testable.

Services

For now, let's create a service based on our parkingFactoryFunction function, as follows:

parkingApp.js var parking = angular.module("parking", []); parkingService.js parking.factory("parkingService", function () { var _calculateTicket = function (car) { var departHour = car.depart.getHours(); var entranceHour = car.entrance.getHours(); ...

Get AngularJS Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.