BRollinson
BRollinson

Reputation: 11

$scope undefined when testing with Karma

I am a complete beginner with testing AngularJS so ANY suggestions are appreciated!

I am getting an error when i try and run Karma Start that looks like this

Chrome 54.0.2840 (Mac OS X 10.11.6) DashboardCtrl Should Exist FAILED
Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- DashboardCtrl
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20DashboardCtrl
    at node_modules/angular/angular.js:68:12
    at node_modules/angular/angular.js:4511:19
    at Object.getService [as get] (node_modules/angular/angular.js:4664:39)
    at node_modules/angular/angular.js:4516:45
    at getService (node_modules/angular/angular.js:4664:39)
    at injectionArgs (node_modules/angular/angular.js:4688:58)
    at Object.instantiate (node_modules/angular/angular.js:4730:18)
    at $controller (node_modules/angular/angular.js:10369:28)
    at node_modules/angular-mocks/angular-mocks.js:2221:12
    at Object.<anonymous> (www/tests/dashboard.spec.js:9:19)
Error: Declaration Location
    at window.inject.angular.mock.inject (node_modules/angular-mocks/angular-mocks.js:3047:25)
    at Suite.<anonymous> (www/tests/dashboard.spec.js:7:13)
    at www/tests/dashboard.spec.js:1:1
Expected undefined to be defined.
    at Object.<anonymous> (www/tests/dashboard.spec.js:13:25)

My Dashboard Controller looks like this

angular.module('myApp.controllers', [])
.controller('DashboardCtrl', function($rootScope, $scope, API, $ionicModal, $window, $ionicLoading) {

With some content and all the required closing tags below ;)

My Test code looks like this

describe('DashboardCtrl', function(){
var $controller, DashboardCtrl;

beforeEach(angular.mock.module('ui.router'));
beforeEach(angular.mock.module('myApp.controllers'));

beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
    DashboardCtrl = $controller('DashboardCtrl', {});
}));

it('Should Exist', function(){
    expect(DashboardCtrl).toBeDefined();
})
});

I have seen some other posts on this but i just can't figure out where i am going wrong, thanks for any help in advance everyone!

Upvotes: 1

Views: 325

Answers (1)

Dennis Baizulin
Dennis Baizulin

Reputation: 1420

You are forgetting to create and inject $scope

describe('DashboardCtrl', function(){
    var $scope,
        DashboardCtrl;

    beforeEach(angular.mock.module('ui.router'));
    beforeEach(angular.mock.module('myApp.controllers'));

    beforeEach(inject(function($rootScope, $controller){
        $scope = $rootScope.$new();

        DashboardCtrl = $controller('DashboardCtrl', {
            $scope: $scope
        });
    }));

    it('Should Exist', function(){
        expect(DashboardCtrl).toBeDefined();
    })
});

Upvotes: 1

Related Questions