user1413969
user1413969

Reputation: 1291

Unknown scope provider angularjs unit test

I tried to do something very similar to the example on egghead.io. https://egghead.io/lessons/angularjs-testing-a-controller

So essentially the test is working, but it stops working once I injected $scope and $rootScope into the controller.

var myApp = angular.module("formulaViewer", []);

myApp.controller("AppCtrl", function($scope, $rootScope) {
    this.message = "Hello";
})

Test file

describe("sup", function() {
var appCtrl;
beforeEach(module("formulaViewer"));
beforeEach(inject(function ($controller) {
    appCtrl = $controller("AppCtrl");
}));

describe("AppCtrl", function() {
    it("should pass", function () {
        expect(appCtrl.message).toBe("Hello");
    });
});
});

I even tried adding $scope, and $rootScope in the beforeEach(inject(function ($controller) part, which still didn't work.

This is the error message:

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope http://errors.angularjs.org/1.2.26/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope

Upvotes: 0

Views: 1003

Answers (1)

aarosil
aarosil

Reputation: 4898

Below is how I got it working.

If this doesn't work in your scenario can you show the non-working test where you're injecting $rootScope and getting that error message?

describe("sup", function() {
var appCtrl, $scope; // create the var for $scope 
beforeEach(module("formulaViewer"));
beforeEach(inject(function ($controller, $rootScope) {  // inject $rootScope
    appCtrl = $controller("AppCtrl");
    $scope = $rootScope;   // assign injected $rootScope to $scope 
}));

describe("AppCtrl", function() {
    it("should pass", function () {
        expect(appCtrl.message).toBe("Hello");
    });
});
});

Hope it helps.

Upvotes: 1

Related Questions