【问题标题】:Angular Karma test not running properly, gives error "Argument 'fn' is not a function, got undefined"Angular Karma 测试运行不正常,给出错误“参数 'fn' 不是函数,未定义”
【发布时间】:2015-02-06 16:39:15
【问题描述】:

我正在尝试使用 karma 将单元测试添加到角度控制器,但我遇到了一个问题,我需要一些帮助。我在这里发现了类似的问题,但没有一个让我得到答案。我尝试更改 karma.conf.js 中“文件”数组的顺序,认为它们可能没有正确加载,但没有产生任何结果。当我运行“karma start karma.conf.js”时出现错误

来自业力的控制台错误

Error: [$injector:modulerr] Failed to instantiate module undefined due to:
Error: [ng:areq] Argument 'fn' is not a function, got undefined
at assetArg (D:/xxxx/bower_components/angular/angular.js:1580
...
...
...  (long callstack within angular.js and angular-mocks.js)
...
...
at workFn (D:/xxxx/bower_components/angular-mocks/angular-mocks.js:2172

我现在只使用一个测试文件,它非常基础。模块名称正确,并且 'myApp.views.view1` 文件正在由 karma 服务器提供服务,“调试”级别的 karma 记录确认了这一点

view1_test.js

'use strict';

describe('myApp.views.view1 module', function() {

  beforeEach(module('myApp.views.view1',[]));

  describe('view1 controller', function(){

    it('should exist....', inject(function($controller) {
      //spec body
      var view1Ctrl = $controller('View1Ctrl');
      expect(view1Ctrl).toBeDefined();
    }));

  });
});

这是它正在测试的视图控制器

view1.js

'use strict';

angular.module('myApp.views.view1', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1', {
    templateUrl: 'src/views/view1/view1.html',
    controller: 'View1Ctrl'
  });
}])
.controller('View1Ctrl', ['$scope','$anchorScroll',function($scope,$anchorScroll) {
    //scroll to top of page
    $anchorScroll();

    $scope.testVar = "it works, congrats!";


}]);

目录结构

/
.bowerrc
.gitignore
bower.json
package.json
karma.conf.js
/sass_styles
    base.scss
/app
    index.html
    /src
         app.js
         /bower_components
         /directives
             /example
                 directive.js
                 template.html
         /views
             /view1
                 view1.html
                 view1.js
                 view1_test.js
         /styles
             base.css

这是我的 karma.conf.js 文件。我使用 PhantomJS 作为浏览器和 html2js 预处理器来包含使用 'templateURL' 的指令 .html 模板

karma.conf.js

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

        basePath : './app/',

        preprocessors: {
            'src/directives/**/*.html': ['ng-html2js']
        },

        files : [
            'src/bower_components/angular/angular.js',
            'src/bower_components/angular-route/angular-route.js',
            'src/bower_components/angular-mocks/angular-mocks.js',
            'src/directives/**/*.js',
            'src/views/**/*.js',
            //directive templates
            'app/src/directives/**/**.html'
        ],

        autoWatch : true,

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

        frameworks: ['jasmine'],

        browsers : ['PhantomJS'],

        plugins : [
                'karma-chrome-launcher',
                'karma-firefox-launcher',
                'karma-phantomjs-launcher',
                'karma-jasmine',
                'karma-junit-reporter',
                'karma-ng-html2js-preprocessor'
            ],

        junitReporter : {
          outputFile: '../test_out/unit.xml',
          suite: 'unit'
        },

        ngHtml2JsPreprocessor: {
            // strip app from the file path
            stripPrefix: 'app/',
            stripSufix: '.ext',
            // prepend this to the 
            prependPrefix: 'served/',

            // setting this option will create only a single module that contains templates 
            // from all the files, so you can load them all with module('foo') 
            moduleName: 'templates'
        }


  });
};

对于复制粘贴大量内容深表歉意,但这是一个复杂的过程,我想确保我不会遗漏一小部分。那么我在这里做错了什么导致了这个错误?

【问题讨论】:

    标签: javascript angularjs jasmine karma-runner karma-jasmine


    【解决方案1】:

    问题在于,当您在规范文件测试设置中获取模块时,实际上是通过指定第二个参数来创建它,因此它只是清理在该模块下注册的所有内容。

    变化:

     beforeEach(module('myApp.views.view1',[]));
    

     beforeEach(module('myApp.views.view1'));
    

    并且当您使用$controller 实例化控制器时,您需要提供范围,因为您在控制器中注入$scope 并且不存在$scopeProvider,它是一个特殊的依赖注入,所以您需要提供它。

    类似这样的:

    it('should exist....', inject(function($controller, $rootScope) {
      //spec body
      var scope = $rootScope.$new();
      var view1Ctrl = $controller('View1Ctrl', {$scope:scope});
      expect(view1Ctrl).toBeDefined();
    }));
    

    【讨论】:

    • 好收获。但现在我面临一个新错误(当然)“错误:[$injector:unpr] 未知提供者:$scopeProvier
    • 您需要在本地提供范围。我更新我的答案
    • 完美,感谢您的帮助。我为此挣扎了一段时间
    猜你喜欢
    • 2015-10-20
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 2015-11-21
    • 1970-01-01
    相关资源
    最近更新 更多