【发布时间】:2021-04-12 17:07:52
【问题描述】:
我正在尝试了解如何在编写 Angular 应用程序时采用测试驱动开发方法;我一直在关注Introduction to Angular Test Driven Development 教程。
在我编写任何测试之前,我在第一关就失败了。
当我从tutorial/ 目录运行karma start 时,我收到错误:
Karma v 6.3.2 - 已连接; test: karma_error 在 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 和app.js 中的files: [] 行应该可以访问它。目前我的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
})
}
【问题讨论】:
标签: angular karma-runner