1

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!

5
  • You have to inject the dependencies into the controller Commented Nov 2, 2016 at 15:28
  • Thanks, that got rid of the error, however, it has stopped my $rootScope functions from working in the controller, just wondering if i need to inject rootScope aswell? if so is this done differently? Commented Nov 2, 2016 at 16:05
  • if (! $rootScope.isSessionActive()) { $window.location.href = (''); } Commented Nov 2, 2016 at 16:27
  • the above is the code that is failing, and this is defined in the services.js $rootScope.isSessionActive = function () { return $window.localStorage.token && $window.localStorage.username ? true : false; } Commented Nov 2, 2016 at 16:28
  • @BRollinson have you tried my solution? Commented Nov 16, 2016 at 13:16

1 Answer 1

1

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();
    })
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.