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

Pha*_*Tan 4 javascript angularjs karma-jasmine

我在茉莉花的小型单位测试与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: {
    },
Run Code Online (Sandbox Code Playgroud)

的index.html

<body ng-app="material" ng-controller="AppController">
    <ui-view></ui-view>
</body>
Run Code Online (Sandbox Code Playgroud)

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');
})
Run Code Online (Sandbox Code Playgroud)

的LoginController

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

      $scope.name = "Ari";
      $scope.sayHello = function() {
         $scope.greeting = "Hello " + $scope.name;
      }
});
Run Code Online (Sandbox Code Playgroud)

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");
      });

})
Run Code Online (Sandbox Code Playgroud)

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

Mad*_*esh 8

检查角度模块的加载顺序.作为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'
],
Run Code Online (Sandbox Code Playgroud)