Angular Karma测试运行不正常,给出错误“Argument'fn'不是函数,未定义”

时间:2015-02-06 16:39:15

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

我正在尝试使用业力将单位测试添加到角度控制器,但我遇到了一个问题,我需要一些帮助。我在这里发现了类似的问题,但没有一个让我得到答案。我已经尝试改变karma.conf.js中“files”数组的顺序,认为它们可能没有正确加载但是没有产生任何结果。当我运行'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'
        }


  });
};

为复制粘贴大量内容而道歉,但这是一个涉及的过程,我想确保我不会错过一小部分。那么我在这里做错了什么导致了这个错误?

1 个答案:

答案 0 :(得分:1)

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

变化:

 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();
}));
相关问题