我对在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',
});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
感谢任何人的帮助,我对此感到非常困惑。
发布于 2016-07-21 20:22:35
试试这个:
'use strict';
angular.module('app')
.service('testService', [ function(URLRoute) {
var superAwesomeFunction = function() {
var URLRequest = URLRoute;
console.log(URLRequest);
};
return {
superAwesomeFunction: superAwesomeFunction
};
}]);获取自:lostechies
https://stackoverflow.com/questions/38500981
复制相似问题