Angularjs $controller服务在加载控制器时引发错误

Angularjs $controller服务在加载控制器时引发错误,angularjs,jasmine,karma-jasmine,Angularjs,Jasmine,Karma Jasmine,我用的是角度1.5.8。这是我的密码: describe('My Controller', function() { var MyController; var $controller; var $rootScope; var $state; beforeEach(angular.mock.module('ui.router')); beforeEach(module('app.my.ctrl')); beforeEach(inject(

我用的是角度1.5.8。这是我的密码:

describe('My Controller', function() {
    var MyController;
    var $controller;
    var $rootScope;
    var $state;

    beforeEach(angular.mock.module('ui.router'));
    beforeEach(module('app.my.ctrl'));
    beforeEach(inject(function(_$controller_, _$rootScope_, _$state_) {
        $controller = _$controller_;
        $rootScope = _$rootScope_;
        $state = _$state_;
        MyController = $controller('MyController', { scope: $rootScope.$new() });
    }));

    describe('#init', function() {
        it('should do something', function() {
            console.log('logStatement', MyController);

            MyController.init();

            expect(true).toBe(true);
        })

    })
});
测试运行程序能够定位所有文件,因此这不是忘记加载某个文件的情况。当我运行此测试时,不仅
logStatement
从未出现,我还收到以下错误:

Argument 'MyController' is not a function, got undefined
这是我的控制器:

(function() {
'use strict';

angular
    .module('app.my.ctrl')
    .controller('MyController', MyController);

MyController.$inject = [
    '$scope'
];
/* ngInject */
function MyController($scope) {

    var vm = this;

    vm.hello = 'world';

    vm.init = function() {
        return true;
    }
}

})();
这是我的karma conf文件:

// Karma configuration

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'bower_components/angular-ui-router/release/angular-ui-router.js',
      'src/controllers/MyController.js',
      'tests/unit/**/*.spec.js',
    ],


    // list of files to exclude
    exclude: [
      '**/*.swp'
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['spec'],

    //  Spec Reporter Config
    specReporter: {
    //     suppressErrorSummary: false,
    //     suppressFailed: false,
    //     suppressPassed: false,
        suppressSkipped: true
    //     showSpecTiming: false
    },


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
};
这是什么意思??我在文档中找不到任何可以解释这一点的东西

更新:


我已阅读,但答案无效。

正在尝试将控制器中注入的服务从
scope
更改为
$scope

beforeEach(inject(function(_$controller_, _$rootScope_, _$state_) {
      $controller = _$controller_;
      $rootScope = _$rootScope_;
      $state = _$state_;
      MyController = $controller('MyController', { $scope: $rootScope.$new()});
}));

您是否尝试使pb尽可能简单?在应用程序内部,您可以成功地进行此调用吗<代码>$controller('MyController',{$scope:$rootScope.$new()})。如果这是可行的(事实上应该如此),那么问题肯定来自于你的测试/茉莉花/卡玛/咕噜声/咕噜声配置,你不应该再钻研角度方向了。您能让我们看看您是如何在应用程序中定义
app.my.ctrl
模块的吗?也许这个模块依赖于更多的模块,而不仅仅是您在测试中模拟的
ui.router
。如果是这种情况,则无法加载模块,也无法创建其中的任何控制器。

控制器所定义的模块的名称是什么?请阅读代码段<代码>'app.my.ctrl'我读到了,我只是想确定这是正确的。是的,它是正确的。发布你的
karma.conf.js
和你的控制器的定义。您正在添加
角度模拟库吗?这肯定是OP将遇到的下一个问题,但不是当前问题:)请仅添加已测试的答案。使用您的代码对我有效,除了此答案。我建议您检查您的controller.js文件是否已在karma过程中加载。为此,请在url中打开浏览器**“使用
karma start--no-single run
命令。请在控制器a console.log中的“use strict”行后按示例打印,或检查控制器是否已加载到Chrome开发工具的
Sources
选项卡中。@dopatraman,此答案可能不包含整个答案,但肯定的是,Gonzalo是正确的,您没有使用正确的属性名称注入作用域。因此,依赖项注入器不可能将“scope”属性与“$scope”控制器参数相匹配。这将以你的控制器创建失败而告终。“没有骰子”。。。我不明白。这是否意味着测试上下文的简单调用不起作用?或者这是否意味着您的模块不包含更多依赖项。(关于信息,模块定义是您在帖子中没有提到的唯一重要内容)
describe('My Controller', function() {
  var MyController;
  var scope;

  beforeEach(angular.mock.module('ui.router'));
  beforeEach(module('app.my.ctrl'));
  beforeEach(inject(function($rootScope) {
      scope = $rootScope.$new();
  }));

  describe('#init', function() {
      it('should do something', function($componentController) {
          var MyController = $componentController('MyController', {
                $scope : scope
            });
          MyController.init();

          expect(true).toBe(true);
      })

  })
});