本文介绍了角/噶:未知提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的角和发现很难测试,以了解如何做我的控制器注入一个简单的测试,得到了错误:
错误:[$喷油器:unpr]未知提供商:localStorageServiceProvider< - localStorageService
控制器:
angular.module('myApp.home',[])
.controller('HomeCtrl',['$范围,localStorageService',函数($范围,localStorageService){
// ...
}]);
测试:
描述('myApp.home模块',函数(){
变量$范围;
VAR localStorageService; beforeEach(模块('myApp.home')); 描述(家居控制器',函数(){ 它('应该....',注(函数($控制器,_ $ rootScope _,_ localStorageService_){
。$ SCOPE = _ $ rootScope _ $新();
localStorageService = _localStorageService_; VAR headerCtrl = $控制器('HomeCtrl',{$范围:$范围,localStorageService:localStorageService});
期待(headerCtrl).toBeDefined();
})); });
});
在我的 karma.conf.js
引用我:
文件:
应用程序/ bower_components /角/ angular.js',
应用程序/ bower_components /角路由/角route.js',
应用程序/ bower_components /角嘲笑/角mocks.js',
应用程序/ bower_components /角本地存储/距离/角本地storage.js',
应用程序/ JS /控制器/ *。JS
]
解决方案
假设你正在使用这个库https://github.com/grevory/angular-local-storage
该模块未加载测试。
要么根本
angular.module('myApp.home',['LocalStorageModule'])
将模块注入到你的模块。
或尝试
beforeEach(模块('LocalStorageModule'));
beforeEach(模块('myApp.home'));
I am new to testing in Angular and finding difficult to understand how to do a simple test on my controller's injections, getting the error:
Error: [$injector:unpr] Unknown provider: localStorageServiceProvider <- localStorageService
Controller:
angular.module('myApp.home', [])
.controller('HomeCtrl', ['$scope','localStorageService',function($scope,localStorageService) {
// ...
}]);
Test:
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();
}));
});
});
In my karma.conf.js
i referenced:
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'
]
解决方案
Assuming you are using this library https://github.com/grevory/angular-local-storage
The module is not loaded for the test.
Either do
angular.module('myApp.home', ['LocalStorageModule'])
to inject the module into your module.
or try
beforeEach(module('LocalStorageModule'));
beforeEach(module('myApp.home'));
这篇关于角/噶:未知提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!