Javascript $scopeProvider <- $scope/ 未知提供者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26591402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$scopeProvider <- $scope/ Unknown provider
提问by Foker
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
我用 jasmine( http://jasmine.github.io/2.0/)测试我的 angular 应用程序并收到下一个错误:Unknown provider: $scopeProvider <- $scope 我知道,在过滤器中使用作用域构建依赖项是不正确的,服务、工厂等,但我在控制器中使用 $scope!为什么我收到这个错误?控制器看起来像
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');
}));
});
回答by Swoogan
You need to manually pass in a $scopeto your controller:
您需要手动将 a 传递$scope给您的控制器:
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');
expect(testCont.upper('jumpEr')).toEqual('JUMPER');
...
});
});
});
回答by Dan Ochiana
Normally, a $scope will be available as an injectable param only when the controller is attached to the DOM.
通常,仅当控制器附加到 DOM 时,$scope 才可用作可注入参数。
You need to associate somehow the controller to the DOM (I'm mot familiar with jasmine at all).
您需要以某种方式将控制器与 DOM 相关联(我根本不熟悉茉莉花)。
回答by Guy
I am following a video tutorial from egghead (link bellow) which suggest this approach:
我正在关注egghead(链接如下)的视频教程,其中建议了这种方法:
describe("hello world", function () {
var appCtrl;
beforeEach(module("app"))
beforeEach(inject(function ($controller) {
appCtrl = $controller("AppCtrl");
}))
describe("AppCtrl", function () {
it("should have a message of hello", function () {
expect(appCtrl.message).toBe("Hello")
})
})
})
Controller:
控制器:
var app = angular.module("app", []);
app.controller("AppCtrl", function () {
this.message = "Hello";
});
I am posting it because in the answer selected we are creating a new scope. This means we cannot test the controller's scope vars, no?
我发布它是因为在选择的答案中,我们正在创建一个新的范围。这意味着我们不能测试控制器的范围变量,不是吗?
link to video tutorial (1min) : https://egghead.io/lessons/angularjs-testing-a-controller
视频教程链接(1 分钟):https: //egghead.io/lessons/angularjs-testing-a-controller

