繁体   English   中英

单元测试AngularJS:未捕获错误:[$ injector:nomod]模块不可用! 和ReferenceError:运行Karma时未定义模块

[英]Unit-Testing AngularJS: Uncaught Error: [$injector:nomod] Module is not available! and ReferenceError: module is not defined when run Karma

我在茉莉花的一个小单元测试与Karma一起跑。 但是,当我运行Karma时,它显示错误:

未捕获错误:[$ injector:nomod]模块'material.controllers'不可用! 您要么错误拼写了模块名称,要么忘记加载它。 如果注册模块,请确保将依赖项指定为第二个参数。

FAILED单元:LoginController遇到声明异常

ReferenceError:未定义模块

这是我的源代码,配置Karma文件和unitTest。

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: [
      '/home/tanpham/Angular/Dev/libs/angular/angular.js',
      '/home/tanpham/Angular/Dev/js/**/*.js',
      '/home/tanpham/Angular/Dev/js/*.js',
      '/home/tanpham/Angular/Test/*.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: {
    },

的index.html

<body ng-app="material" ng-controller="AppController">
    <ui-view></ui-view>
</body>

app.js

angular.module('material.controllers', []);
angular.module('material.services', []);
angular.module('material.directives',[]);
angular.module('material.filters',[]);
var app = angular.module('material', ['ui.router','material.directives','http-auth-interceptor','material.controllers','material.services','material.filters'])
.config(function($stateProvider, $urlRouterProvider,$locationProvider) {
    $stateProvider
    .state('app', {
        url: "/app",
        abstract: true,
        templateUrl: "views/app.html"
    }).state('login', {
        url: "/login",
        templateUrl: "views/login.html",
        controller: "LoginController"
    });

    $urlRouterProvider.otherwise('/login');
})

的LoginController

angular.module('material.controllers').controller('LoginController', function($scope) {

      $scope.name = "Ari";
      $scope.sayHello = function() {
         $scope.greeting = "Hello " + $scope.name;
      }
});

helloSpec.js

describe('Unit: LoginController', function() {

      // Load the module with LoginController
      beforeEach(module('material.controllers'));

      var ctrl, scope;
      // 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('LoginController', {
          $scope: scope
        });
      }));

      it('should create $scope.greeting when calling sayHello', 
        function() {
          expect(scope.greeting).toBeUndefined();
          scope.sayHello();
          expect(scope.greeting).toEqual("Hello Ari");
      });

})

所以,我可以做到这一点,我的模块名称是对的?

检查角度模块的加载顺序。 作为karma conf中javascript文件的列表,查看模块定义文件是否先于其他使用它的文件加载。

调整此清单中的顺序,显式加载定义'material.controllers'模块的文件。

files: [
  '/home/tanpham/Angular/Dev/libs/angular/angular.js',
  '/home/tanpham/Angular/Dev/js/**/*.js',
  '/home/tanpham/Angular/Dev/js/*.js',
  '/home/tanpham/Angular/Test/*.js'
],

我遇到过这个问题。 并解决了它。 只需在模块声明语句中的模块名称后面添加“,[]”。在这种情况下,chaged代码将是:

angular.module('material.controllers',[])
 .controller('LoginController', function($scope) {

  $scope.name = "Ari";
  $scope.sayHello = function() {
     $scope.greeting = "Hello " + $scope.name;
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM