kimpettersen
kimpettersen

Reputation: 1602

Injecting controller gives empty object AngularJS

I have an AngularJS app, that I am trying to test. The problem is that whenever I try to inject a controller it gives me an empty object.

beforeEach( inject(function($rootScope, $controller) {
   console.log($controller) // {}
   scope = $rootScope.$new();
   ctrl = $controller('MenuCtrl', {
     $scope: scope
   });
   console.log(ctrl) // {}
}));

I have attached a sample of my setup with some comments in a JsFiddle, and I would really appreciate some help to figure this out.

Fiddle

edit: I have also tried to do this:

beforeEach(angular.mock.module('Controllers'));

Then I get: Error: No module: Controllers

This fixed that problem:

beforeEach(angular.mock.module('controllers'));

Upvotes: 2

Views: 1065

Answers (2)

kimpettersen
kimpettersen

Reputation: 1602

Thanks to Peter Bacon Darwin on the Angular mailing list for finding the solution to this. I was trying to call changeView() on the controller. However, it is the scope that has this function and not the controller.

Thanks again.

Upvotes: 2

Michelle Tilley
Michelle Tilley

Reputation: 159135

Based on your code,

var Controllers = angular.module('controllers', []);

your test configuration

beforeEach(angular.mock.module('Controllers'));

should instead be

beforeEach(angular.mock.module('controllers'));

with a lowercase 'C', as that is the actual name of this module. You could also use myApp, as it depends on controllers.

Upvotes: 0

Related Questions