Javascript 角度模块不可用Karma Jasmine

Javascript 角度模块不可用Karma Jasmine,javascript,angularjs,karma-jasmine,Javascript,Angularjs,Karma Jasmine,我是业力新手,所以这个错误可能是非常基本的 这是我的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: ht

我是业力新手,所以这个错误可能是非常基本的

这是我的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
  })
}
app目录中
my
js
html
文件驻留

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>
(function(angular) {
  'use strict';
    angular.module('heroApp', []).controller('MainCtrl', function MainCtrl() {
        this.hero = {
            name: 'Miles Bronson'
        };
    });
})(window.angular);
现在,在我的
测试规范
文件中,我尝试了:

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

})
但是它说
chrome55.0.2883(windows8.10.0.0)有错误
未捕获错误:[$injector:nomod]模块“heroApp”不可用!您要么拼错了模块名,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。
http://errors.angularjs.org/1.6.1/$injector/nomod?p0=heroApp
位于bower_components/angular/angular.js:2183


我做错了什么?

这可能是因为没有指定文件加载顺序,karma只是按字母顺序加载源文件,因此
herodail.js
排在第一位,而
heroApp
模块是在
index.js
中定义的。尝试如下更改您的业力配置:

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'
],

令人惊叹的。救了那个头痛的人!作为一个初学者,这对我来说是新的。非常感谢。@StruglingCoder欢迎您。如果我的回答有助于您解决问题,请将其标记为已接受