2015-11-03 11 views
7

Sto provando a provare un servizio AngularJS carService, ma il $httpBackend non sembra funzionare.

//carService 
angular.module('services').factory('carService', 
    function($http) { 
     return { 
      getTypes: function() { 
       return $http.get('/api/cars/types'); 
      } 
     }; 
    }); 

Qualcuno può spiegare perché la risposta è nullo?

describe("Services", function() { 

    beforeEach(module("app.services")); 

    describe("Car services", function() { 

     var service, $httpBackend; 

     beforeEach(inject(function($injector) { 
      service = $injector.get('carService'); 
      $httpBackend = $injector.get('$httpBackend'); 

      $httpBackend.when('GET', "/api/cars/types").respond(["Toyota", "Honda", "Tesla"]); 
     })); 

     afterEach(function() { 
      $httpBackend.verifyNoOutstandingExpectation(); 
      $httpBackend.verifyNoOutstandingRequest(); 
     }); 

     it('getTypes - should return 3 car manufacturers', function() { 
      service.getTypes().then(function(response) { 
       expect(response.length).toEqual(3); //the response is null 
      }); 
      $httpBackend.flush(); 
     }); 


    }); 
}); 

risposta

7

Prova questo:

expect(response.data.length).toEqual(3); 

L'oggetto risposta restituita da una richiesta $http ha i dati di risposta entro il data proprietà (docs).

Problemi correlati