notAChance
notAChance

Reputation: 1430

Karma - Unknown Provider: $scopeProvider

I'm getting - Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope when running the following test:

describe('mainCtrl', function () {

beforeEach(module('app'));
var controller, scope;
var window = {
    open: function (url, target, specs) {
        var spec, specKey;
        this.href = url;
        this.target = target;
        // Parse through the spec string to grab the parameters you passed through
        var specArray = specs.split(',');
        for (specKey in specArray) {
            spec = specArray[specKey].split('=');
            this[String.trim(spec[0])] = String.trim(spec[1]);
        }
    }
};

beforeEach(inject(function ($controller, $window, $rootScope) {
    scope = $rootScope.$new();
    controller = $controller('mainCtrl', {$scope: scope});
    window = $window;
}));

describe('$scope.popup1', function () {

    it('should open a popup window when ISIN hyperlink is clicked within grid, passing ISIN object s values to shareDataService', inject(function ($window, $scope) {
        spyOn($window, 'open').and.callFake(function () {
            return true;
        });
        scope.popup1()
        expect(window.href).toEqual("views/Box_Ladder.html");
        expect(window.target).toEqual("_blank");
        expect(window.height).toEqual(400);
        expect(window.width).toEqual(700);
    })
   )
})
});

But I have no idea why. I have injected scope (as far as I can see) and included angular-mocks in my karma.conf.js file.

Upvotes: 4

Views: 1265

Answers (1)

tasseKATT
tasseKATT

Reputation: 38490

It's because you are trying to inject $scope into the it function:

it('should open a popup window ...', inject(function ($window, $scope)

Just remove it and it should work:

it('should open a popup window ...', inject(function ($window)

Just like the error states, there is no $scopeProvider. When testing you have to manually create scopes and assign them, just like you have done when creating the controller:

scope = $rootScope.$new();
controller = $controller('mainCtrl', {$scope: scope});

Upvotes: 4

Related Questions