From 1e2cb6a2d22c0ad231199f5b73b9381b2079f9b2 Mon Sep 17 00:00:00 2001 From: gustavo Date: Sun, 27 Sep 2015 02:21:17 +0200 Subject: [PATCH] showcase: how to test REST service --- .../src/showcase/app/cars/cars.service.spec.js | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) 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 eb39451..4c7f454 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.service.spec.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.service.spec.js @@ -29,43 +29,53 @@ describe('app.cars', function() { describe('cars service', function () { it('should invoke GET all cars without error', function () { - $httpBackend.expectGET(API.CARS).respond(200, carsResponseSuccess); + var respValue; + $httpBackend.expectGET(API.CARS).respond(200, carsResponseSuccess); cars.getAll().then(function(resp) { - expect(carsResponseSuccess).toEqual(resp); + respValue = resp; }); - $httpBackend.flush(); + + expect(carsResponseSuccess).toEqual(respValue); }); it('should invoke GET all cars with error', function () { - $httpBackend.expectGET(API.CARS).respond(400, carsResponseError); + var reasonValue; + $httpBackend.expectGET(API.CARS).respond(400, carsResponseError); cars.getAll().then(function() {}, function(reason) { - expect(carsResponseError).toEqual(reason); + reasonValue = reason; }); - $httpBackend.flush(); + + expect(carsResponseError).toEqual(reasonValue); }); it('should invoke GET car by id without error', function () { - $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(200, carResponseSuccess); + var respValue; + $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(200, carResponseSuccess); cars.getById(id).then(function(resp) { - expect(carResponseSuccess).toEqual(resp.data); + respValue = resp.data; }); $httpBackend.flush(); + + expect(carResponseSuccess).toEqual(respValue); }); it('should invoke GET car by id with error', function () { - $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(400, carsResponseError); + var reasonValue; + $httpBackend.expectGET(API.CAR.replace(':carId', id)).respond(400, carsResponseError); cars.getById(id).then(function() {}, function(reason) { - expect(carsResponseError).toEqual(reason.data); + reasonValue = reason.data; }); $httpBackend.flush(); + + expect(carsResponseError).toEqual(reasonValue); }); }); -- 2.1.4