简体   繁体   English

Angular模块不可用Karma Jasmine

[英]Angular Module is not available Karma Jasmine

I am new to Karma , so the error might be very basic. 我是Karma的新手,因此错误可能非常基本。

It is my karma.conf.js file 这是我的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: [
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'bower_components/angular-resource/angular-resource.js',
      'app/*.js',
      'test/*.js',
      'app/**/*.js',
      '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: {
    },


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


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


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

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

Inside app directory my js and html files reside. 在我的jshtml文件所在的app directory

在此处输入图片说明

The index.html looks like : index.html看起来像:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-heroComponentSimple-production</title>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>

  <script src="index.js"></script>
  <script src="heroDetail.js"></script>

</head>
<body ng-app="heroApp">
  <!-- components match only elements -->
<div ng-controller="MainCtrl as ctrl">
  <b>Hero</b><br>
  <hero-detail hero="ctrl.hero"></hero-detail>
</div>
</body>
</html>

And index.js looks like : 而且index.js看起来像:

(function(angular) {
  'use strict';
    angular.module('heroApp', []).controller('MainCtrl', function MainCtrl() {
        this.hero = {
            name: 'Miles Bronson'
        };
    });
})(window.angular);

Now in my test spec file I tried : 现在在我的test spec文件中,我尝试了:

describe('MainController',function(){
    var $rootScope,
        $scope,
        controller;
    beforeEach(function(){
        module('heroApp');

        inject(function($injector){
            $rootScope = $injector.get('$rootScope');
            $scope = $rootScope.$new();
            controller = $injector.get('$controller')('MainCtrl',{$scope: $scope});
        });
    });

    describe('Initialization',function(){
        it('should initialize name of the hero',function(){
            expect('$scope.hero.name').toEqual('Miles Bronson'); 
        });
    });

})

But it said Chrome 55.0.2883 (Windows 8.1 0.0.0) ERROR Uncaught Error: [$injector:nomod] Module 'heroApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.6.1/$injector/nomod?p0=heroApp at bower_components/angular/angular.js:2183 但是它说Chrome 55.0.2883 (Windows 8.1 0.0.0) ERROR Uncaught Error: [$injector:nomod] Module 'heroApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.6.1/$injector/nomod?p0=heroApp at bower_components/angular/angular.js:2183 Chrome 55.0.2883 (Windows 8.1 0.0.0) ERROR Uncaught Error: [$injector:nomod] Module 'heroApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.6.1/$injector/nomod?p0=heroApp at bower_components/angular/angular.js:2183 Chrome 55.0.2883 (Windows 8.1 0.0.0) ERROR Uncaught Error: [$injector:nomod] Module 'heroApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.6.1/$injector/nomod?p0=heroApp at bower_components/angular/angular.js:2183 . Chrome 55.0.2883 (Windows 8.1 0.0.0) ERROR Uncaught Error: [$injector:nomod] Module 'heroApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.6.1/$injector/nomod?p0=heroApp at bower_components/angular/angular.js:2183

What am I doing wrong ? 我究竟做错了什么 ?

This is probably because files loading order is not specified and karma just loads your source files in alphabetical order, therefore heroDetail.js comes first, but heroApp module is defined in index.js . 这可能是因为未指定文件加载顺序,而业力只是按字母顺序加载源文件,因此, heroDetail.js在第一位,但heroApp模块在index.js定义。 Try to change your karma config like this: 尝试像这样更改您的业力配置:

files: [
  'bower_components/angular/angular.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'bower_components/angular-resource/angular-resource.js',
  'app/index.js',
  'app/heroDetail.js',
  'test/*.js',
  'app/**/*.js',
  'test/**/*.js'
],

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

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