Angular 运行 Karma 测试时未加载(遵循教程)

Angular not loading when running Karma tests (following a tutorial)

我正在尝试了解如何在编写 Angular 应用程序时采用测试驱动开发方法;我一直在关注 Introduction to Angular Test Driven Development 教程。

在我编写任何测试之前,我在第一个障碍上失败了。

当我从 tutorial/ 目录 运行 karma start 时,出现错误:

Karma v 6.3.2 - connected; test: karma_error An error was thrown in afterAll Uncaught ReferenceError: angular is not defined ReferenceError: angular is not defined at http://localhost:9876/base/app/app.js

如何加载 Angular?

我目前的理解是 angular 应该自动加载,因为 karma.conf.js 中的 files: [] 行并且 app.js 应该可以访问它。目前,我的 app.js 正在创建一个基本控制器,仅此而已,据我所知,我的代码与本阶段教程中的代码相匹配。该教程有一个 Git Hub repository.

根据教程,我的目录结构如下:

tutorial/
|-app/
|---app.js
|-karma.conf.js
|-node_modules/ [Only listing relevant files]
|---angular/
|------angular.js
|---angular-mocks/
|------angular-mocks.js
|-package.json
|-tests/
|--[empty dir]

这是我的全部app/app.js:

angular.module('ItemsApp', [])
    .controller('MainCtrl', function($scope) {
        $scope.title = 'Items App Title'
    });

这是我的 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: [
      'app/**.js',
      'tests/**.js',
      'node_modules/angular/angular.js',
      'node_modules/angular-mocks/angular-mocks.js'
    ],


    // list of files / patterns 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
  })
}

当 运行 karma init.

时,本教程建议您以错误的顺序提供应用程序的路径

由于app/app.js依赖于node_modules/angular/angular.js,后者应该先声明。要解决此问题,请更新 karma.conf.js 以声明:

    files: [
      'node_modules/angular/angular.js',
      'node_modules/angular-mocks/angular-mocks.js',
      'app/**.js',
      'tests/**.js'
    ],

这是教程示例文件中显示的顺序,但不是我按照 运行 karma init 教程中的说明生成的(在我的系统上)顺序。