16.07.2017 Views

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 />

return [204, "", {}];<br />

});<br />

parkingHttpFacade.deleteCar(1).success(function (data, status) {<br />

expect(data).toBe("");<br />

expect(status).toBe(204);<br />

expect(mockedCars.getNumberOfCars()).toBe(1);<br />

});<br />

$httpBackend.flush();<br />

});<br />

});<br />

Each response is configured through the respond function which takes a function<br />

that contains the method, url, data, and headers parameters of the request<br />

function. This function returns an array with three elements: the first is the status<br />

code, the second is the body, and the third is the header inside an object literal.<br />

Also, as the parkingHttpFacade function returns a promise from each operation,<br />

which delegates to the $http service; you have to use the flush function of the<br />

$httpBackend service to dispatch the pending requests.<br />

To simulate the interaction with the database, we also provided a mockedCars object,<br />

created through the buildMockedCars factory function, as follows:<br />

mockedCarsFactoryFunction.js<br />

var buildMockedCars = function () {<br />

var _cars = [<br />

{<br />

plate: "AAA9999",<br />

color: "Blue"<br />

},<br />

{<br />

plate: "AAA9988",<br />

color: "White"<br />

}<br />

];<br />

var _getCars = function () {<br />

return _cars;<br />

};<br />

var _getCar = function (id) {<br />

return _cars[_id(id)];<br />

};<br />

[ 135 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!