From 6bad1d598399b7b03233dc0c2b384c43976785b1 Mon Sep 17 00:00:00 2001 From: gustavo Date: Sat, 26 Sep 2015 00:43:48 +0200 Subject: [PATCH] showcase: how to test REST service --- .../showcase/src/showcase/app/cars/cars.service.js | 2 + .../src/showcase/app/cars/cars.service.spec.js | 48 ++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/angularjs/showcase/src/showcase/app/cars/cars.service.js b/angularjs/showcase/src/showcase/app/cars/cars.service.js index f69f0b3..6b1c7cd 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.service.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.service.js @@ -219,6 +219,8 @@ * @name getById * @methodOf app.cars.cars * + * @param {number} id Car's identifier number. + * * @description * Get one car by its id from API REST. */ diff --git a/angularjs/showcase/src/showcase/app/cars/cars.service.spec.js b/angularjs/showcase/src/showcase/app/cars/cars.service.spec.js index 846e416..eb39451 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.service.spec.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.service.spec.js @@ -3,6 +3,15 @@ describe('app.cars', function() { var $httpBackend; var $log; + var id = 666; + var carResponseSuccess = { + content: 'Car: false', + id: id + }; + var carsResponseSuccess = [ + carResponseSuccess + ]; + var carsResponseError = 'Error: connect ECONNREFUSED'; var cars; var API; @@ -19,13 +28,46 @@ describe('app.cars', function() { describe('cars service', function () { - it('should invoke GET all cars', function () { - $httpBackend.expectGET(API.CARS).respond({}); + it('should invoke GET all cars without error', function () { + $httpBackend.expectGET(API.CARS).respond(200, carsResponseSuccess); - cars.getAll(); + cars.getAll().then(function(resp) { + expect(carsResponseSuccess).toEqual(resp); + }); $httpBackend.flush(); }); + + it('should invoke GET all cars with error', function () { + $httpBackend.expectGET(API.CARS).respond(400, carsResponseError); + + cars.getAll().then(function() {}, function(reason) { + expect(carsResponseError).toEqual(reason); + }); + + $httpBackend.flush(); + }); + + it('should invoke GET car by id without error', function () { + $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(200, carResponseSuccess); + + cars.getById(id).then(function(resp) { + expect(carResponseSuccess).toEqual(resp.data); + }); + + $httpBackend.flush(); + }); + + it('should invoke GET car by id with error', function () { + $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(400, carsResponseError); + + cars.getById(id).then(function() {}, function(reason) { + expect(carsResponseError).toEqual(reason.data); + }); + + $httpBackend.flush(); + }); + }); afterEach(function() { -- 2.1.4