0

我對AngularJS測試非常新,我感到困惑。我有一個控制器依賴於依賴於某個值的服務。我已經爲控制器編寫了非常基本的測試,以便掌握它,並且當我沒有向服務注入值時它們都會通過。但是,當我將該值注入服務時,我在控制器的所有測試中得到了Unknown provider錯誤。用依賴性測試angularjs

我的代碼是下面:

控制器
'use strict'; 
angular.module('app') 
    .controller('testController', function($scope, testService) { 

     $scope.$on('$ionicView.enter', function() { 
      $scope.setup(); 
     }); 

     $scope.testFunction = function() { 
      $scope.result = 1; 
     }; 

     $scope.setup = function() { 
      testService.superAwesomeFunction(); 
      $scope.enterValue = 1; 
     }; 

    }); 

服務
'use strict'; 
angular.module('app') 
    .service('testService', ['URLRoute', function(URLRoute) { 

     var superAwesomeFunction = function() { 
      var URLRequest = URLRoute; 
      console.log(URLRequest); 
     }; 

     return { 
      superAwesomeFunction: superAwesomeFunction 
     }; 
    }]); 

'use strict'; 
angular.module('app') 
    .value('URLRoute', { 
     url: 'https://abcdefghijklmnop.com/api', 
    }); 

test.controller.test.js
'use strict'; 
describe('testController', function() { 
    beforeEach(module('app')); 

    var $controller, 
     $rootScope, 
     $scope; 

    beforeEach(inject(function(_$controller_, _$rootScope_) { 
     $controller = _$controller_; 
     $rootScope = _$rootScope_; 
     $scope = $rootScope.$new(); 

     $controller('testController', { 
      $scope: $scope 
     }); 
    })); 

    describe('$scope.$on(ionicView.enter)', function() { 
     it ('$scope.$on calls $ionicView.enter', function() { 
      spyOn($scope, '$on').and.callThrough(); 
      $scope.$on('$ionicView.enter'); 
      expect($scope.$on).toHaveBeenCalledWith('$ionicView.enter'); 
     }); 
    }); 

    describe('$scope.testFunction', function() { 
     it('Sets the value of result to 1', function() { 
      $scope.testFunction(); 
      expect($scope.result).toEqual(1); 
     }); 
    }); 

    describe('$scope.setup', function() { 
     it('Sets up stuff for the view after it has loaded', function() { 
      $scope.setup(); 
      expect($scope.enterValue).toEqual(1); 
     }); 
    }); 


}); 

當測試現在運行,我得到的錯誤是:

Unknown provider: URLRouteProvider <- URLRoute <- testService

讚賞任何幫助,一直很困惑與這一個。

回答

0

試試這個:

'use strict'; 
angular.module('app') 
.service('testService', [ function(URLRoute) { 

    var superAwesomeFunction = function() { 
     var URLRequest = URLRoute; 
     console.log(URLRequest); 
    }; 

    return { 
     superAwesomeFunction: superAwesomeFunction 
    }; 
}]); 

得到它:lostechies

+0

呀,所以這是不是有效的角度,但我覺得你的意思是去掉方括號與縮小它,並試圖那樣?如果是這樣,則會導致相同的錯誤 –