Shamoon
Shamoon

Reputation: 43639

AngularJS Testing Controller with Karma

My Controller is:

angular.module('mean').controller('ItemsController', ['$scope', function ($scope) {
  $scope.contentTemplate = '/views/items/index.html';
  $scope.subMenu = [
    {name: 'Create Item', location: '/items/create'}
  ];
}]);

My test is pretty simple:

  describe('ItemsController', function () {
    var scope;

    beforeEach(module('mean'));

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

    }));


    it('should have sub menu items loaded properly', function () {
      expect(scope.subMenu.length).toBe(1);
    });

  });

What I want is to test that there is one subMenu item. Instead, the error I get is:

PhantomJS 1.9.7 (Mac OS X) ItemsController should have sub menu items loaded properly FAILED TypeError: 'undefined' is not a function (evaluating '$rootScope.new()')

Isn't $rootScope injected? So why is it undefined?

Upvotes: 6

Views: 2687

Answers (1)

Rob
Rob

Reputation: 11733

You want the method which starts with dollar sign:

scope = $rootScope.$new();
//                 ^

That should fix it.

Upvotes: 19

Related Questions