AngularJS未知提供者:$ scopeProvider

时间:2015-02-26 20:27:12

标签: angularjs angularjs-scope

我在控制器中尝试使用$ scope服务时遇到问题。控制器基本上来自角度种子项目。

'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 个答案:

答案 0 :(得分:7)

必须是这样的(您在第二次调用$scope时错过$controller参数):

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


  });
});

答案 1 :(得分:1)

在测试中,您实例化控制器而不传递$scope

$controller("NameAddressController");

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