我有一个angularjs工厂,我正在向该工厂注入下划线,应用程序运行良好,但当我尝试在上面编写jasmine测试用例时,我收到了一个错误,没有找到下划线提供程序我有像一样的工厂
angular.module("sample")
.factory("example", example);
example.$inject = ["$document", "$compile", "$rootScope", "$timeout", "$q", "underscore"];
function example($document, $compile, $rootScope, $timeout, $q, _) {
}
我把我的模块定义为
(function(){
angular.module(samlple,[]);
})();
我的测试用例是
beforeEach(module('sample'));
beforeEach(module('ionic'));
beforeEach(inject(function ($document, $compile, $rootScope, $timeout,underscore,example) {
}
它的给出错误错误:[$injector:unp]未知提供程序:undercoreProvider<-下划线
在index.html中添加导入下划线,然后将其作为服务添加。
var underscore = angular.module('underscore', []);
underscore.factory('_', function() {
return window._; // assumes underscore has already been loaded on the page
});
和
//Now we can inject underscoreJS in the controllers
function MainCtrl($scope, _) {
//using underscoreJS method
_.max([1,2,3,4]); //It will return 4, which is the maximum value in the array
}
但我建议你用洛达什!它有更酷的功能。关于如何在Angular中使用lodash的信息,你可以在这里找到。
Piggybacking@Bakhtier的回答,我用以下方法让Karma/Jastine识别出lodash,这样我就可以在我的服务以及我的应用程序的其他部分中使用它。
angular.module('app', ['app.services', 'lodash']);
angular.module('app.services', ['lodash']).factory('MyService', ['_', function (_){
// your code bits
}]);
angular.module('lodash', []).factory('_', function() {
return window._;
});
希望这能帮助到别人。