AngularJS未知提供者:$ scopeProvider

Dou*_*ger 3 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;
    }
]);
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的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();
        }));


    });
});
Run Code Online (Sandbox Code Playgroud)

当我在这里运行测试时,我得到的错误和堆栈跟踪:

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
Run Code Online (Sandbox Code Playgroud)

mei*_*lke 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();
    }));


  });
});
Run Code Online (Sandbox Code Playgroud)