我在尝试在我的控制器中使用$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
ng
。发布完整的堆栈跟踪。并且发布测试代码。 - JB Nizet