AngularJS controller with karma

My controller:

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); }); }); 

I want to check that there is one subMenu element. Instead, an error occurs:

PhantomJS 1.9.7 (Mac OS X) ItemsController must have submenu items correctly loaded FAILED TypeError: 'undefined' is not a function (evaluate to "$ rootScope.new ()")

Is $ rootScope being entered? So why is it undefined?

+6
source share
1 answer

You need a method starting with a dollar sign:

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

That should fix it.

+19
source

All Articles