Umair Jameel Umair Jameel - 9 days ago 6
AngularJS Question

Error: Unknown provider: $scopeProvider <- $scope in karma-jasmine

My controller:

angular.module('dr.search.components',[])
.controller('HomeCtrl', function ($scope, $state, $timeout, $interval, $rootScope) {
// code
});


Testing Code (inside describe):

var HomeController, $controller;
var scope, $state, $timeout, $interval,$rootScope;

beforeEach(module('dr.search.components'));
beforeEach(module('ui.router'));

beforeEach(inject(function(_$controller_, _$scope_, _$state_, _$timeout_, _$interval_, _$rootScope_) {
$controller = _$controller_;
scope = _$rootScope_.$new();
$state = _$state_;
$timeout = _$timeout_;
$interval = _$interval_;
$rootScope = _$rootScope_;

HomeController = $controller('HomeCtrl', {$scope: scope, $state: $state, $timeout: $timeout, $interval: $interval, $rootScope: $rootScope});

}));

// Verify our controller exists
it('Home Controller should be defined', function() {
expect(HomeController).toBeDefined();
});


I am testing my controller, if it exists or not. It is showing me Unknown provider: $scopeProvider <- $scope.

Answer

Remove $scope from your beforeEach.

beforeEach(inject(function(_$controller_, _$state_, _$timeout_, _$interval_, _$rootScope_) {
    ...
}));
Comments