gpt4 book ai didi

javascript - $scopeProvider <- $scope/未知提供者

转载 作者:行者123 更新时间:2023-11-28 19:40:05 25 4
gpt4 key购买 nike

我用 Jasmine (http://jasmine.github.io/2.0/)测试我的 Angular 应用程序并得到下一个错误:未知提供者:$scopeProvider <- $scope我知道,在过滤器、服务、工厂等中构建范围依赖是不正确的,但我在 Controller 中使用 $scope!为什么我会收到此错误? Controller 看起来像

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 = {

};
}]);

和我的测试代码:

'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');
}));
});

最佳答案

您需要手动将 $scope 传递给您的 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');
expect(testCont.upper('jumpEr')).toEqual('JUMPER');
...
});
});
});

关于javascript - $scopeProvider <- $scope/未知提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591402/

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