2015-07-08 1 views
2

У меня есть сервис:

(function() { 
    angular.module('app').service('MyAppService', MyAppService); 
    MyAppService.$inject = ['$http', 'testUrl']; 
    function MyAppService($http, testUrl) { 
     var service = { 
      testFunction: testFunction   
     }; 
     return service; 
     function testFunction() { 
      /*testurl is my backend API*/ 
      return $http.get(testUrl)     
       .error(function(){ 
        return; 
       }) 
       .then(function (response) { 
        return response.data; 
       }); 
     } 
    } 
})(); 

я называю это в мой контроллер, как:

 testControllerFunction(); 
     function testControllerFunction() { 
      MyAppService.testFunction().then(function (response) { 
       app.testResponse = response; //This my http response 
       console.log(app.testResponse); 
      }); 
     } 

Я пишу тест-карма случае успешного запроса $ http.get в MyAppService как:

describe('MyAppService', function() { 
     var MyAppService,http; 
     beforeEach(function() { 
      module('app'); 
      inject(function ($injector) { 
       MyAppService = $injector.get('MyAppService'); 
       testUrl = $injector.get('testUrl'); 
       http = $injector.get('$httpBackend'); 
      }); 
     }); 

     it('should call the backend testurl ', function() { 
      MyAppService.testFunction(); 
      http.expectGET(testUrl); 
     }); 
    }); 

Но это, похоже, не работает? Где я неправ? Спасибо!

ответ

1

Вы должны flush$httpBackend

it('should call the backend testurl ', function() { 
     http.expectGET(testUrl);    
     MyAppService.testFunction(); 
     http.flush();   
    });