'use strict';

describe('CityService', function() {
    var service, q, scope, http;

    beforeEach(function() {
        module('ddsApp', function($provide) {
            $provide.value('$http', {
                get: function(url) {
                    // Prevent mocking real $http calls
                    if (! url.match(/\d{5}$/)) {
                        return http.get(url);
                    }

                    var deferred = q.defer();
                    var data = [{
                        codeInsee: '75119',
                        codePostal: '75019',
                        nomCommune: 'paris 19',
                    }];
                    if (url && url.slice && url.slice(-5) === '33610') {
                        data = [{
                            codeInsee: "33090",
                            codePostal: "33610",
                            nomCommune: "CANEJAN",
                        },
                        {
                            codeInsee: "33122",
                            codePostal: "33610",
                            nomCommune: "CESTAS",
                        }];
                    }
                    deferred.resolve({ data: data });
                    return deferred.promise;
                },
            });
        });
        inject(function($rootScope, $q, $http, CityService) {
            scope = $rootScope.$new();
            service = CityService;
            http = $http;
            q = $q;
        });
    });

    describe('function getCities()', function() {
        it('Should return single array for 75019', function(done) {
            // when
            var result = service.getCities('75019');

            // then
            result.then(function(cities) {
                expect(cities.length).toBe(1);
                done();
            });
            scope.$apply();
        });

        it('Should return a two-element array for 33610', function(done) {
            // when
            var result = service.getCities('33610');

            // then
            result.then(function(cities) {
                expect(cities.length).toBe(2);
                done();
            });
            scope.$apply();
        });
    });
});

@@ -66,22 +66,20 @@
 ce, 
-q, scope, http
+$httpBackend
 ;%0A%0A 
@@ -132,26 +132,56 @@
 App'
-, function($provid
+);%0A        inject(function($injector, CityServic
 e) %7B
@@ -197,329 +197,219 @@
     
-$provide.value('$http', %7B%0A                get: function(url) %7B%0A                    // Prevent mocking real $http calls%0A                    if (! url.match(/%5Cd%7B5%7D$/)) %7B%0A                        return http.get(url);%0A                    %7D%0A%0A                    var deferred = q.defer();%0A                    var data = %5B%7B%0A    
+service = CityService;%0A%0A            $httpBackend = $injector.get('$httpBackend');%0A%0A            $httpBackend%0A                .whenGET('/api/outils/communes/75019')%0A                .respond(200, JSON.stringify(%5B%7B%0A
     
@@ -456,36 +456,32 @@
                 
-    
 codePostal: '750
@@ -497,36 +497,32 @@
                 
-    
 nomCommune: 'par
@@ -537,39 +537,38 @@
                 
-    %7D%5D;
+%7D%5D));%0A
 %0A               
@@ -568,68 +568,75 @@
     
-        if (url && url.slice && url.slice(-5) === '
+$httpBackend%0A                .whenGET('/api/outils/communes/
 33610')
- %7B
 %0A   
@@ -652,34 +652,40 @@
     
-        data = %5B%7B%0A        
+.respond(200, JSON.stringify(%5B%7B%0A
     
@@ -736,32 +736,24 @@
             
-        
 codePostal: 
@@ -777,32 +777,24 @@
             
-        
 nomCommune: 
@@ -816,32 +816,24 @@
             
-        
 %7D,%0A         
@@ -843,25 +843,10 @@
     
-        %7B%0A       
+%7B%0A
     
@@ -853,33 +853,32 @@
                 
- 
 codeInsee: %223312
@@ -881,24 +881,16 @@
 33122%22,%0A
-        
         
@@ -938,32 +938,24 @@
             
-        
 nomCommune: 
@@ -984,369 +984,14 @@
     
-        %7D%5D;%0A                    %7D%0A                    deferred.resolve(%7B data: data %7D);%0A                    return deferred.promise;%0A                %7D,%0A            %7D);%0A        %7D);%0A        inject(function($rootScope, $q, $http, CityService) %7B%0A            scope = $rootScope.$new();%0A            service = CityService;%0A            http = $http;%0A            q = $q;
+%7D%5D));%0A
 %0A   
@@ -1342,32 +1342,33 @@
             %7D);%0A
+%0A
             scop
@@ -1359,36 +1359,42 @@
             
-scope.$apply
+$httpBackend.flush
 ();%0A        
@@ -1693,24 +1693,25 @@
         %7D);%0A
+%0A
             
@@ -1714,20 +1714,26 @@
     
-scope.$apply
+$httpBackend.flush
 ();%0A

Use $httpBackend to mock HTTP calls.