Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Argument 'MainCtrl' is not a function, got undefined

I've just installed Yeoman and created my first project. I'm trying to get my head around unit testing the default angular project that is created by Yo. The project runs fine when I invoke "grunt server", but when I run the unit test "grunt test:unit" I get the error "Argument 'MainCtrl' is not a function, got undefined".

What am i missing to get this test running properly?

Thanks again

-- controller

'use strict';

angular.module('myApp')
.controller('MainCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma',
'mytest'
  ];
});

-- unti test

 'use strict';

 describe('Controller: MainCtrl', function () {

 // load the controller's module
 beforeEach(module('myApp'));

var MainCtrl,
 scope;

  // Initialize the controller and a mock scope
 beforeEach(inject(function ($controller, $rootScope) {
  scope = $rootScope.$new();
 MainCtrl = $controller('MainCtrl', {
 $scope: scope
});
}));

it('should attach a list of awesomeThings to the scope', function () {
expect(scope.awesomeThings.length).toBe(4);
});
});
like image 265
user686483 Avatar asked Oct 20 '22 22:10

user686483


1 Answers

If I copy and paste this code it is executed without problems. The error message you're getting indicates that the controller (name) isn't known to the injector. Did you forget to include the controller javascript file in the karma.conf.js?

like image 177
null Avatar answered Oct 30 '22 22:10

null