AngularJS未知提供者:$scopeProvider。

3

我在尝试在我的控制器中使用$scope服务时遇到了问题。该控制器基本上是从angular-seed项目中获取的。

'use strict';

angular.module('regApp.nameAddress', ['ngRoute'])
.config([
    '$routeProvider', function($routeProvider) {
        $routeProvider.when('/nameAddress', {
            templateUrl: 'views/NameAddress.html',
            controller: 'NameAddressController'
        });
    }
])
.controller('NameAddressController', ['$scope', 
    function($scope) {
        $scope.x = 1;
    }
]);

这里是我正在使用的Jasmine测试代码:

'use strict';

describe('regApp.nameAddress module', function() {

    beforeEach(function() { module('regApp.nameAddress') });

    describe('Name and Address Controller', function () {


        var scope;
        var httpBackend;

        beforeEach(inject(function ($rootScope, $controller, $injector) {
            scope = $rootScope.$new();
            httpBackend = $injector.get("$httpBackend");

            $controller("NameAddressController", {
                $scope: scope
            });
        }));


        it('should exist', inject(function($controller) {
            //spec body
            var sut = $controller("NameAddressController");
            expect(sut).toBeDefined();
        }));


    });
});

当我运行测试时,这是我收到的错误和堆栈跟踪:

15 specs, 1 failure Spec List | Failures 

regApp.nameAddress module Name and Address Controller should exist

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope  
http://errors.angularjs.org/1.2.28/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope 
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope  
http://errors.angularjs.org/1.2.28/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope 
    at Anonymous function (http://localhost:30489/js/lib/angular/angular.js:3801:13)     
    at getService (http://localhost:30489/js/lib/angular/angular.js:3929:11)  
    at Anonymous function (http://localhost:30489/js/lib/angular/angular.js:3806:13)     
    at getService (http://localhost:30489/js/lib/angular/angular.js:3929:11)  
    at invoke (http://localhost:30489/js/lib/angular/angular.js:3953:9)    
    at instantiate (http://localhost:30489/js/lib/angular/angular.js:3976:7)     
    at Anonymous function (http://localhost:30489/js/lib/angular/angular.js:7315:7)     
    at Anonymous function (http://localhost:30489/specs/NameAddressController_tests.js:25:13)    
    at invoke (http://localhost:30489/js/lib/angular/angular.js:3965:7)    
    at workFn (http://localhost:30489/js/lib/angular-mocks/angular-mocks.js:2177:11) 
undefined

2
移除隐式依赖于 ng。发布完整的堆栈跟踪。并且发布测试代码。 - JB Nizet
2个回答

7

您需要像这样进行操作(在第二次调用$controller时,您错过了$scope参数):

'use strict';

describe('regApp.nameAddress module', function() {

  var sut;
  beforeEach(function() { module('regApp.nameAddress') });

  describe('Name and Address Controller', function () {


    var scope;
    var httpBackend;

    beforeEach(inject(function ($rootScope, $controller, $injector) {
        scope = $rootScope.$new();
        httpBackend = $injector.get("$httpBackend");

        sut = $controller("NameAddressController", {
            $scope: scope
        });
    }));


    it('should exist', inject(function($controller) {
        //spec body
        expect(sut).toBeDefined();
    }));


  });
});

这个和JB的答案都是正确的,而且这个恰好阐述了我所需要的。 - Doug Seelinger

1
在你的测试中,你实例化控制器时没有传递 $scope
$controller("NameAddressController");

不允许这样做。$scope 不是一个可注入的服务。必须明确地创建并传递给控制器,就像你在 beforeEach() 函数中所做的那样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接