Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 795
Shamoon Avatar asked Feb 09 '14 01:02

Shamoon


1 Answers

You want the method which starts with dollar sign:

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

That should fix it.

like image 78
Rob Avatar answered Nov 06 '22 16:11

Rob