Я новичок в тестировании в AngularJS, и я сбиваюсь с толку. У меня есть контроллер, который зависит от службы, которая зависит от значения. Я написал очень простые тесты для контроллера, чтобы справиться с ним, и все они проходят, когда у меня нет значения, введенного в службу. Однако, когда я ввожу значение в службу, я получаю ошибку Unknown provider
во всех тестах для контроллера.
Мой код ниже:
'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',
});
'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
Любая помощь оценивалась, очень смущалась с этим.
Попробуй это:
'use strict';
angular.module('app')
.service('testService', [ function(URLRoute) {
var superAwesomeFunction = function() {
var URLRequest = URLRoute;
console.log(URLRequest);
};
return {
superAwesomeFunction: superAwesomeFunction
};
}]);
Получил это: lostechies