运行单元测试:Karma+Jasmine给出未定义的函数“控制器”错误



我正在尝试在现有的angular项目中实现单元测试。为此,我添加了Grunt Karma

karma:
      unit:
          options:
              frameworks: ['jasmine'],
              singleRun: true,
              browsers: ['PhantomJS'],
              files: [bower js files + project dev/test js files]

控制器是

angular.module('app.lol.ctrls', []).controller('LOLCtrl', [
    '$scope', '$filter', 'Resource', '$log', function($scope, $filter, Resource, $log) {//some logic}

测试规范为

describe('Controller: LOLCtrl', function () {
    beforeEach(module('app'));
    var OrderCtrl;
    var scope;
    var filter;
    var log;
    var resource=someResourceWithSomeDataFunc;
    beforeEach(inject(function ($controller, $rootScope, $filter, $log) {
        scope = $rootScope.$new();
        OrderCtrl = $controller('LOLCtrl', {
            $scope: scope,
            $filter: filter,
            Resource: resource,
            $log: log
        });
    }));
    it('should have lolVar to be undefined', function () {
        expect(scope.lolVar).toBeUndefined();
    });
});

当我运行测试时,我得到错误

PhantomJS 1.9.8 (Linux 0.0.0) Controller: LOLCtrl should have lolVar to be undefined FAILED
    Error: [ng:areq] Argument 'LOLCtrl' is not a function, got undefined
    http://errors.angularjs.org/1.3.20/ng/areq?p0=LOLCtrl&p1=not%20a%20function%2C%20got%20undefined
    undefined
        at assertArg ....

我尝试了一些解决方案,比如在beforeEach中使用angular.mock.module而不是module。此外,我还仔细检查了是否包含控制器文件。此外,CCD_ 4被注入到CCD_ 5本身中。我尝试了beforeEach(module(app.lol.ctrls)),但也出现了同样的错误。我们将不胜感激。

首先我想您忘记了包含模块的依赖项。在不知道你的应用程序代码的完整结构的情况下,我想你定义控制器的代码应该是

angular.module('app.lol.ctrls', ['app.lol', '/*maybe other dependecies*/']).controller('LOLCtrl', [ ]);

或者你有自己的模块文件?那么它应该是这样的:

angular.module('app.lol.ctrls').controller('LOLCtrl', [ ]);

在解决了这些依赖性问题后,请查看您的index.html。是否包含
<script src="bower_components/angular-mocks/angular-mocks.js"></script>
如果没有,安装有角度的模型并包括它。

然后,作为第二步,转到您的测试规范,并添加前面的更改。每个部分如下所示:

beforeEach(function(){
    module('app.lol.ctrls');
    module('ngMockE2E');
});

请尝试再次运行测试。如果你遇到了和以前一样的错误,看看你的grunt文件。因果报应包含的你的文件列表对我来说有点奇怪。也许这也可能是个问题。为了寻求帮助,这里有一个从我的咕哝文件中截取的片段:

karma: {  
        options: {
            frameworks: ['jasmine'],
            files: [
                '<%= dom_munger.data.appjs %>',
                    //this files data is also updated in the watch handler, if updated change there too
                    'bower_components/angular-mocks/angular-mocks.js',
                    'application/**/*-spec.js',
                    'application/**/*.html',
                    'base/**/*-spec.js',
                    'error/**/*.html',
                    'html/**/*.html',
                    '*.html',
                    'bower_components/**/*.html'
            ],
            preprocessors: {
                '**/*.html': ['ng-html2js']
            },
            ngHtml2JsPreprocessor: {
                moduleName: 'templates'
            },
            logLevel: 'WARN',
            reporters: ['mocha'],
            captureConsole: true,
            autoWatch: false,
            singleRun: true
        }
  }

我希望这能帮你解决一点问题。

最新更新