Angularjs,Karma控制器单元测试注入器:modulerr

Angularjs & Karma controller unit test injector:modulerr

本文关键字:注入器 modulerr 单元测试 控制器 Karma Angularjs      更新时间:2023-09-26

为了应用一些单元测试到我的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();
    });
})

我在karma服务器日志上得到了这个错误:

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与注入在那里,所以我认为问题是在依赖注入,但在哪里?…

修改代码:

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

:

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

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

p。作为一般提示,在调试时尝试使用未合并的angular.js而不是angular.min.js,它会给您一个更有意义的错误。

EDIT:原来还有一个问题:

lodashunderscore库缺失,Restangular需要