我是Angular测试的新手,发现很难理解如何对我的控制器进样进行简单的测试,得到错误:
Error: [$injector:unpr] Unknown provider: localStorageServiceProvider <- localStorageService
控制器:
angular.module('myApp.home', [])
.controller('HomeCtrl', ['$scope','localStorageService',function($scope,localStorageService) {
// ...
}]);
测试
describe('myApp.home module', function() {
var $scope;
var localStorageService;
beforeEach(module('myApp.home'));
describe('home controller', function(){
it('should ....', inject(function($controller,_$rootScope_,_localStorageService_) {
$scope = _$rootScope_.$new();
localStorageService = _localStorageService_;
var headerCtrl = $controller('HomeCtrl',{"$scope" : $scope, "localStorageService" : localStorageService});
expect(headerCtrl).toBeDefined();
}));
});
});
在我引用的karma.conf.js
中:
files : [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-local-storage/dist/angular-local-storage.js',
'app/js/controllers/*.js'
]
答案 0 :(得分:23)
假设您正在使用此库https://github.com/grevory/angular-local-storage
未加载模块进行测试。
要么
angular.module('myApp.home', ['LocalStorageModule'])
将模块注入模块。
或尝试
beforeEach(module('LocalStorageModule'));
beforeEach(module('myApp.home'));