【问题标题】:Angularjs & Karma controller unit test injector:modulerrAngularjs & Karma 控制器单元测试注入器:modulerr
【发布时间】:2014-09-28 05:23:50
【问题描述】:

为了对我的 AngularJS 控制器应用一些单元测试,我无法让它正常工作...

这是我的 Angularjs 应用声明:

    var ogcApp = angular.module('OGC', [
    'ngAnimate',
    'xeditable',
    'ngRoute',
    'ogc.services',
    'ogc.controllers',
    'restangular',
    'AngularStomp',
    'ui.bootstrap',
    'ogc.directives',
    'mgcrea.ngStrap',
    'angularFileUpload',
    'jlareau.pnotify',
    'ngTagsInput'
]);

这是我的控制器原型:

angular.module('ogc.controllers').controller('applicantController', function ($scope, $log, $location, $cacheFactory, ngstomp, identityService, Restangular) { ...Some code... }

这是我的 karma.conf.js :

    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: [
        'src/main/webapp/static/js/lib/angular/angular.min.js',
        'src/main/webapp/static/js/lib/angular-animate/angular-animate.min.js',
        'src/main/webapp/static/js/lib/angular-xeditable/dist/js/xeditable.min.js',
        'src/main/webapp/static/js/lib/angular-route/angular-route.min.js',
        'src/main/webapp/static/js/lib/restangular/dist/restangular.min.js',
        'src/main/webapp/static/js/lib/AngularStompDK/dist/angular-stomp.min.js',
        'src/main/webapp/static/js/lib/angular-bootstrap/ui-bootstrap-tpls.min.js',
        'src/main/webapp/static/js/lib/angular-strap/dist/angular-strap.min.js',
        'src/main/webapp/static/js/lib/ng-file-upload/angular-file-upload.min.js',
        'src/main/webapp/static/js/lib/jquery/dist/jquery.min.js',
        'src/main/webapp/static/js/lib/pnotify/pnotify.core.js',
        'src/main/webapp/static/js/lib/ng-tags-input/ng-tags-input.min.js',
        'src/main/webapp/static/js/lib/angular-mocks/angular-mocks.js',
        'src/main/webapp/static/js/*.js',
        'src/test/karma/**/*.spec.js'
    ],


    // list of files to exclude
    exclude: [

    ],


    // 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: ['progress'],


    // 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_DEBUG,


    // 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: ['PhantomJS'],


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

这是我的简单测试:

    describe('UNIT : applicantController', function() {

    beforeEach(module('OGC'));

    var ctrl, scope, aSearchForm;
    // inject the $controller and $rootScope services
    // in the beforeEach block
    beforeEach(inject(function($controller, $rootScope) {
        // Create a new scope that's a child of the $rootScope
        scope = $rootScope.$new();
        // Create the controller
        ctrl = $controller('ogc.controllers.applicantController', {
            $scope: scope
        });
    }));

    it('should instantiate the controller properly', function () {
        expect(ctrl).not.toBeUndefined();
    });
})

我在业力服务器日志上收到此错误:

PhantomJS 1.9.7 UNIT : applicantController should instantiate the controller properly FAILED
       Error: [$injector:modulerr] Failed to instantiate module restangular due to:
    ReferenceError: Can't find variable: _

还有一个简单的:

 expect(true).toBe(true);

在有注入的 beforeEach 存在时不起作用,所以我认为问题出在依赖注入上,但在哪里?..

【问题讨论】:

    标签: javascript angularjs unit-testing controller karma-runner


    【解决方案1】:

    更改此代码:

    ctrl = $controller('ogc.controllers.applicantController', {
      $scope: scope
    });
    

    仅此:

    ctrl = $controller('applicantController', {
      $scope: scope
    });
    

    angularjs 中还没有真正的命名空间概念。来自不同模块的所有东西都将混合到同一个桶中。

    PS。作为一般提示,在调试时尝试使用未缩小的angular.js 而不是angular.min.js,它会给您带来更有意义的错误。

    编辑:原来还有一个问题:

    lodashunderscore 库缺失,Restangular 需要它。

    【讨论】:

    • 我已经使用此控制器声明进行了测试,现在使用 Karma conf 文件中的 angular.js 我收到此错误:“错误 [$injector:modulerr] 无法实例化模块 restangular,原因是:ReferenceError: Can '找不到变量:_"
    • 您可能忘记在karma.config.js 中包含lodash/underscore 库,它是restangular 所必需的。
    • runTarm,谢谢!我忘记了 _ 和 lodash 链接..现在我还有其他需要解决的依赖问题..
    • 感谢使用非缩小 angular.js 的一般提示!
    猜你喜欢
    • 1970-01-01
    • 2019-02-07
    • 2012-05-20
    • 2015-11-29
    • 2015-10-03
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多