我是新来的角色测试,发现很难理解如何对控制器的注射进行简单的测试,得到错误:
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' ]
假设你正在使用这个库
https://github.com/grevory/angular-local-storage
模块未加载用于测试.
要么
angular.module(‘myApp.home’,[‘LocalStorageModule’])
将模块插入模块.
或尝试
beforeEach(module('LocalStorageModule')); beforeEach(module('myApp.home'));