$ scopeProvider&LT; - $范围/未知提供商 [英] $scopeProvider <- $scope/ Unknown provider
本文介绍了$ scopeProvider&LT; - $范围/未知提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我测试我的角度应用茉莉花( http://jasmine.github.io/2.0/ ),并获得下一个错误:
未知提供商:$ scopeProvider&LT; - $范围
我知道,这是不正确的建立与过滤器,服务,工厂等范围的依赖,但我用$范围,控制器!
为什么会出现这个错误?控制器看起来像
testModule.controller('TestCont',['$过滤器,$范围',函数($过滤器,$范围){ VAR doPrivateShit =功能(){
的console.log(10);
}; this.lol =功能(){
doPrivateShit();
}; this.add =函数(A,B){
返回A + B;
}; this.upper =函数(){
返回$滤波器('大写')(一);
} $ scope.a = this.add(1,2); $ scope.test = 10; $ scope.search = { };
}]);
和我的测试的code:
使用严格的;描述(testModule模块',函数(){
beforeEach(函数(){
模块('testModule');
}); 它('要正确大写',注入(函数($控制器){
VAR testCont = $控制器('TestCont');
期待(testCont.upper('笑'))toEqual('LOL')。
期待(testCont.upper('跳线'))toEqual(JUMPER)。
期待(testCont.upper('123azaza'))toEqual('123AZAZA')。
期待(testCont.upper('111'))toEqual('111')。
}));
});
解决方案
您需要在 $范围,通过手动
到控制器:
描述('testModule模块',函数(){
beforeEach(模块('testModule')); 描述(测试控制器,函数(){
VAR范围,testCont; beforeEach(注(函数($ rootScope,$控制器){
范围= $ rootScope $新的()。
testCont = $控制器('TestCont',{$范围:适用范围});
}); 它('要正确大写',函数(){
期待(testCont.upper('笑'))toEqual('LOL')。
...
});
});
});
I testing my angular-application with jasmine(http://jasmine.github.io/2.0/) and getting next error: Unknown provider: $scopeProvider <- $scope I know, that it's incorrect to build dependency with scope in filters, services, factories, etc., but I use $scope in controller! Why am i getting this error? controller looks like
testModule.controller('TestCont', ['$filter', '$scope', function($filter, $scope){
var doPrivateShit = function(){
console.log(10);
};
this.lol = function(){
doPrivateShit();
};
this.add = function(a, b){
return a+b;
};
this.upper = function(a){
return $filter('uppercase')(a);
}
$scope.a = this.add(1,2);
$scope.test = 10;
$scope.search = {
};
}]);
and my test's code:
'use strict';
describe('testModule module', function(){
beforeEach(function(){
module('testModule');
});
it('should uppercase correctly', inject(function($controller){
var testCont = $controller('TestCont');
expect(testCont.upper('lol')).toEqual('LOL');
expect(testCont.upper('jumpEr')).toEqual('JUMPER');
expect(testCont.upper('123azaza')).toEqual('123AZAZA');
expect(testCont.upper('111')).toEqual('111');
}));
});
解决方案
You need to manually pass in a $scope
to your controller:
describe('testModule module', function(){
beforeEach(module('testModule'));
describe('test controller', function(){
var scope, testCont;
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
testCont = $controller('TestCont', {$scope: scope});
});
it('should uppercase correctly', function(){
expect(testCont.upper('lol')).toEqual('LOL');
...
});
});
});
这篇关于$ scopeProvider&LT; - $范围/未知提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文