最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Karma error Argument 'Controller' is not a function, got undefined - Stack Overflow

programmeradmin5浏览0评论

I got a problem when I tried to test my controller. When I run my test I got an error

Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined   .3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
        at assertArg (/Users/tetianachupryna/project/bower_ponents/angular/angular.js:1577)
        at assertArgFn (/Users/tetianachupryna/project/bower_ponents/angular/angular.js:1588)
        at /Users/tetianachupryna/project/bower_ponents/angular/angular.js:8418
        at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:11
        at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:17
        at /Users/tetianachupryna/project/node_modules/karma-jasmine/lib/adapter.js:184
        at http://localhost:9877/karma.js:185
        at http://localhost:9877/context.html:51

I know that SO is full of similar questions. But I'm a total null in Angular and JS in general, so those answers didn't help me. From similar questions on SO I discovered that my problem is in wrong definition of the controller but I still can't figure out what I did wrong. I've stack and I'm begging for your help.

First of all here is my src/app/index.js file where my module is defined

var app = angular.module('myModule', [
  'ngAnimate',
  'ngSanitize',
  'ngResource',
  'ui.router',
  'pascalprecht.translate',
  'thing1',
  'thing2']);

Here is src/app/controllers/main-controller.js

angular.module('myModule').controller('MainCtrl', [
    '$scope',
    '$state',
    function ($scope, $state) {
      $scope.state = $state;
      //***
      $scope.isBigStep = function isBigStep() {
        return $state.$current.step == 3;
      };    
  }]);

And finally this a file with the test src/spec/controllers/main-controller.spec.js

describe('MainCtrl', function() {
  var scope, $state, createController;

  beforeEach(inject(function ($rootScope, $controller) {
    scope = $rootScope.$new();

    createController = function() {
      return $controller('MainCtrl', {
        '$scope': scope
      });
    };
  }));

  it('should make 3 as current step', function() {
    var controller = createController();
    expect(scope.isBigStep()).toBe(true);
  });
});

In karma config I have all those files

files: [
      'bower_ponents/angular/angular.js',
      'bower_ponents/angular-mocks/angular-mocks.js',
      'src/app/index.js',
      'src/app/controllers/*.js',
      'src/spec/controllers/*.js'
    ],

For run my test I use karma-runner plugin in RubyMine.

I'd be thankful for any help!

I got a problem when I tried to test my controller. When I run my test I got an error

Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined   http://errors.angularjs/1.3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
        at assertArg (/Users/tetianachupryna/project/bower_ponents/angular/angular.js:1577)
        at assertArgFn (/Users/tetianachupryna/project/bower_ponents/angular/angular.js:1588)
        at /Users/tetianachupryna/project/bower_ponents/angular/angular.js:8418
        at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:11
        at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:17
        at /Users/tetianachupryna/project/node_modules/karma-jasmine/lib/adapter.js:184
        at http://localhost:9877/karma.js:185
        at http://localhost:9877/context.html:51

I know that SO is full of similar questions. But I'm a total null in Angular and JS in general, so those answers didn't help me. From similar questions on SO I discovered that my problem is in wrong definition of the controller but I still can't figure out what I did wrong. I've stack and I'm begging for your help.

First of all here is my src/app/index.js file where my module is defined

var app = angular.module('myModule', [
  'ngAnimate',
  'ngSanitize',
  'ngResource',
  'ui.router',
  'pascalprecht.translate',
  'thing1',
  'thing2']);

Here is src/app/controllers/main-controller.js

angular.module('myModule').controller('MainCtrl', [
    '$scope',
    '$state',
    function ($scope, $state) {
      $scope.state = $state;
      //***
      $scope.isBigStep = function isBigStep() {
        return $state.$current.step == 3;
      };    
  }]);

And finally this a file with the test src/spec/controllers/main-controller.spec.js

describe('MainCtrl', function() {
  var scope, $state, createController;

  beforeEach(inject(function ($rootScope, $controller) {
    scope = $rootScope.$new();

    createController = function() {
      return $controller('MainCtrl', {
        '$scope': scope
      });
    };
  }));

  it('should make 3 as current step', function() {
    var controller = createController();
    expect(scope.isBigStep()).toBe(true);
  });
});

In karma config I have all those files

files: [
      'bower_ponents/angular/angular.js',
      'bower_ponents/angular-mocks/angular-mocks.js',
      'src/app/index.js',
      'src/app/controllers/*.js',
      'src/spec/controllers/*.js'
    ],

For run my test I use karma-runner plugin in RubyMine.

I'd be thankful for any help!

Share Improve this question edited Jan 7, 2015 at 16:52 PSL 124k21 gold badges256 silver badges243 bronze badges asked Jan 6, 2015 at 20:55 Tetiana ChuprynaTetiana Chupryna 1,0741 gold badge11 silver badges28 bronze badges 3
  • 2 You are missing you module load. Put beforeEach(module('myModule')); before beforeEach(inject(function ($rootScope, $controller) {? – PSL Commented Jan 6, 2015 at 20:57
  • Thanks, this trick helped. Could you make a standalone answer that I could accept? – Tetiana Chupryna Commented Jan 7, 2015 at 9:12
  • Sure added an answer. Thx!! – PSL Commented Jan 7, 2015 at 15:25
Add a ment  | 

1 Answer 1

Reset to default 14

What you are missing is to add the module in the beforeEach hook for test setup. Otherwise the controller will not be found. So add beforeEach(module('myModule')).

describe('MainCtrl', function() {
  var scope, $state, createController;

  beforeEach(module('myModule')); //<--- Hook module

  beforeEach(inject(function ($rootScope, $controller) {
    scope = $rootScope.$new();

    createController = function() {
      return $controller('MainCtrl', {
        '$scope': scope
      });
    };
  }));

  it('should make 3 as current step', function() {
    var controller = createController();
    expect(scope.isBigStep()).toBe(true);
  });
});
发布评论

评论列表(0)

  1. 暂无评论