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

getCar: _getCar,<br />

saveCar: _saveCar,<br />

updateCar: _updateCar,<br />

deleteCar: _deleteCar<br />

};<br />

});<br />

Inside each test, we defined the expected response to each request. It could be done<br />

through the $httpBackend service, as follows:<br />

parkingHttpFacadeSpec.js<br />

describe("Parking Http Facade Specification", function () {<br />

var parkingHttpFacade, $httpBackend, mockedCars;<br />

beforeEach(module("parking"));<br />

beforeEach(inject(function (_parkingHttpFacade_, _$httpBackend_) {<br />

parkingHttpFacade = _parkingHttpFacade_;<br />

$httpBackend = _$httpBackend_;<br />

mockedCars = buildMockedCars();<br />

}));<br />

it("Should get the parked cars", function () {<br />

$httpBackend.whenGET("/cars").respond(function (method, url, data,<br />

headers) {<br />

return [200, mockedCars.getCars(), {}];<br />

});<br />

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

expect(data).toEqual(mockedCars.getCars());<br />

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

});<br />

$httpBackend.flush();<br />

});<br />

it("Should get a parked car", function () {<br />

$httpBackend.whenGET("/cars/1").respond(function (method, url,<br />

data, headers) {<br />

return [200, mockedCars.getCar(1), {}];<br />

});<br />

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

expect(data).toEqual(mockedCars.getCar(1));<br />

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

});<br />

[ 133 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!