javascript Karma 错误参数“控制器”不是函数,未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27807022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:01:17  来源:igfitidea点击:

Karma error Argument 'Controller' is not a function, got undefined

javascriptangularjsjasminekarma-runnerkarma-jasmine

提问by Tetiana Chupryna

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.org/1.3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined
        at assertArg (/Users/tetianachupryna/project/bower_components/angular/angular.js:1577)
        at assertArgFn (/Users/tetianachupryna/project/bower_components/angular/angular.js:1588)
        at /Users/tetianachupryna/project/bower_components/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.

我知道 SO 充满了类似的问题。但总的来说,我在 Angular 和 JS 中完全是空的,所以这些答案对我没有帮助。从 SO 上的类似问题中,我发现我的问题在于控制器的错误定义,但我仍然无法弄清楚我做错了什么。我已经堆栈了,我在乞求你的帮助。

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

首先这里是我的src/app/index.js文件,其中定义了我的模块

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

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

这是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

最后这是一个带有测试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_components/angular/angular.js',
      'bower_components/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.

为了运行我的测试,我在 RubyMine 中使用了 karma-runner 插件。

I'd be thankful for any help!

我会很感激任何帮助!

回答by PSL

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')).

您缺少的是在 beforeEach 挂钩中添加模块以进行测试设置。否则将找不到控制器。所以添加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);
  });
});