Reputation: 476
I'm having issues attempting to use the $scope service in my controller. The controller is basically taken from the angular-seed project.
'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;
}
]);
Here's the Jasmine test code I'm using:
'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();
}));
});
});
And when I run the test here is the error and stack trace I'm getting:
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
Upvotes: 3
Views: 3836
Reputation: 3280
It has to be like this (you miss the $scope
parameter in your second call to $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();
}));
});
});
Upvotes: 7
Reputation: 691685
In your test, you're instantiating the controller without passing it a $scope
:
$controller("NameAddressController");
That is not allowed. $scope
is not an injectable service. It must be created and passed explicitely to the controller, as you're doing in your beforeEach()
function.
Upvotes: 1