gpt4 book ai didi

angularjs - Angular/karma : unknown provider

转载 作者:行者123 更新时间:2023-12-02 19:02:04 24 4
gpt4 key购买 nike

我是 Angular 测试的新手,发现很难理解如何对 Controller 的注入(inject)进行简单的测试,并出现错误:

Error: [$injector:unpr] Unknown provider: localStorageServiceProvider <- localStorageService

Controller :

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'])

将模块注入(inject)到您的模块中。

或者尝试

 beforeEach(module('LocalStorageModule'));
beforeEach(module('myApp.home'));

关于angularjs - Angular/karma : unknown provider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28167187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com