16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Unit Testing<br />

At the moment, we have instantiated the controller, which calls the retrieveCars<br />

function and interacts with the parkingHttpFacade controller, thus aiming to<br />

interact with the backend and retrieve the cars. The park function also interacts with<br />

the facade in order to park a new car. Also, in case of any error, a property called<br />

message will be defined into the $scope object and will also be tested:<br />

parkingCtrlWithHttpFacadeSpec.js<br />

describe("Parking Controller With Parking Http Facade Specification",<br />

function () {<br />

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

{<br />

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

beforeEach(inject(function ($controller, $rootScope, _$httpBackend_)<br />

$scope = $rootScope.$new();<br />

$controller("parkingCtrlWithParkingHttpFacade", {<br />

$scope: $scope<br />

});<br />

$httpBackend = _$httpBackend_;<br />

mockedCars = buildMockedCars();<br />

}));<br />

it("The cars should be retrieved", function () {<br />

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

headers) {<br />

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

});<br />

$httpBackend.flush();<br />

expect($scope.cars.length).toBe(2);<br />

});<br />

it("The user should be authenticated", function () {<br />

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

headers) {<br />

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

});<br />

$httpBackend.flush();<br />

expect($scope.message).toBe("You must be authenticated!");<br />

});<br />

it("Something should went wrong!", function () {<br />

[ 138 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!