执行业力测试时出错
Posted
技术标签:
【中文标题】执行业力测试时出错【英文标题】:Error while executing karma tests 【发布时间】:2014-07-12 05:32:09 【问题描述】:我正在尝试为我的应用程序运行业力单元测试。该应用程序是使用我公司的自定义 angular yeoman 生成器创建的。 (生成器与 angular-yeoman 生成器非常相似)。
我正在尝试为我的应用程序运行 karma jasmine 测试。但是每当我运行 grunt test
时,我都会不断收到错误消息Chrome 35.0.1916 (Mac OS X 10.9.0) ERROR Uncaught object at /用户//假人 Apps/karma1/app/bower_components/angular/angular.js:1611
警告:任务“karma:unit”失败。使用 --force 继续。
我的 Gruntfile.js 中有以下内容:
karma:
unit:
configFile: 'karma.conf.js',
singleRun: true
在 karma.conf.js 文件中我有:
module.exports = function(config)
config.set(
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'app/bower_components/angular*/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-bootstrap/ui-bootstrap.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'app/bower_components/service-state/js/StateService.js',
'app/scripts/controllers/dashboard.js',
'../test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
);
;
我的 package.json 文件:
"name": "karma1",
"version": "0.0.0",
"dependencies": ,
"devDependencies":
"grunt": "~0.4.1",
"grunt-css": "~0.5.4",
"grunt-contrib-copy": "~0.4.1",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-coffee": "~0.7.0",
"grunt-contrib-uglify": "~0.2.0",
"grunt-contrib-compass": "~0.5.0",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-less": "~0.9.0",
"grunt-contrib-cssmin": "~0.6.0",
"grunt-contrib-connect": "~0.5.0",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-htmlmin": "~0.1.3",
"grunt-contrib-watch": "~0.5.2",
"grunt-autoprefixer": "~0.2.0",
"grunt-usemin": "~0.1.11",
"grunt-rev": "~0.1.0",
"grunt-shell": "~0.2.2",
"grunt-concurrent": "~0.3.0",
"load-grunt-tasks": "~0.1.0",
"grunt-google-cdn": "~0.2.0",
"grunt-ngmin": "~0.0.2",
"time-grunt": "~0.1.0",
"karma-ng-scenario": "^0.1.0",
"grunt-karma": "^0.8.3",
"karma": "^0.12.16",
"karma-ng-html2js-preprocessor": "^0.1.0",
"karma-jasmine": "^0.1.5",
"karma-chrome-launcher": "^0.1.4",
"karma-mocha": "latest",
"chai": "1.4.0",
"karma-script-launcher": "~0.1.0",
"karma-html2js-preprocessor": "~0.1.0",
"karma-requirejs": "~0.2.0",
"karma-coffee-preprocessor": "~0.1.0",
"karma-phantomjs-launcher": "~0.1.0",
"grunt-open": "~0.2.2",
"ng-midway-tester": "2.0.5"
,
"engines":
"node": ">=0.8.0"
,
"scripts":
"test": "grunt test"
我的测试控制器是: '使用严格';
describe('Controller: DashboardCtrl', function ()
// load the controller's module
beforeEach(module('karma1App'));
var DashboardCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller,$rootScope)
scope = $rootScope.$new();
DashboardCtrl = $controller('DashboardCtrl',
$scope: scope
);
));
it('should attach a list of awesomeThings to the scope', function ()
expect(scope.awesomeThings.length).toBe(3);
);
);
我使用 REQUIREJS 加载其他依赖项,但 REQUIRE 仅在指令中使用。作为一个开始,我想测试我的控制器和服务,但我不断收到这个错误。
知道出了什么问题。
谢谢!
【问题讨论】:
@68cherries - 您好,请详细说明您的评论。可能是我错过了一些东西。谢谢 【参考方案1】:我错过的一点是我没有加载角度应用程序。我正在通过配置文件 config.js 加载 Angular 应用程序
/* 全局角度 */
'使用严格';
var karma1App = angular.module('karma1App', [ 'pascalprecht.translate', 'ngResource', 'ngRoute', 'ui.bootstrap']); karma1App.config(['$routeProvider', function ($routeProvider)
$routeProvider .when('/site', activeTabName: 'site', templateUrl: 'views/site.html', controller: 'SiteCtrl' ) .when('/Cases', templateUrl: 'views/Cases.html', controller: 'CasesCtrl' ) .when('/Cases/:caseid', templateUrl: 'views/Case.html', controller: 'CaseCtrl' ) .when('/Alarms', templateUrl: 'views/Alarms.html', controller: 'AlarmsCtrl' ) .when('/Analysis', templateUrl: 'views/Analysis.html', controller: 'AnalysisCtrl' ) .when('/Reports', templateUrl: 'views/Reports.html', controller: 'ReportsCtrl' ) /* .when('/dashboard', activeTabName: 'dashboard', templateUrl: 'views/main.html', controller: 'MainCtrl' )
*/ .when('/admin', templateUrl: 'views/admin.html', 控制器:'AdminCtrl' ) 。否则( 重定向到:'/网站' ); ]);
在 karma.conf.js 我添加了以下脚本标签:
files: [
'app/bower_components/angular*/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-resource/angular-resource.js',
'app/bower_components/angular-cookies/angular-cookies.js',
'app/bower_components/angular-sanitize/angular-sanitize.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/angular-bootstrap/ui-bootstrap.js',
'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
'./test/test-main.js',
'app/scripts/controllers/dashboard.js',
'../test/spec/**/*.js'
]
test-main.js 仅包含以下代码行:
/* global angular */
'use strict';
var karma1App = angular.module('karma1App', [
'ngResource',
'ngRoute', // Angular 1.2 requires separate ngRoute
'ui.bootstrap',
'StateService'
]);
在 karma.conf.js 文件中加载 test-main.js 文件后,我能够使用 karma 对我的应用程序进行单元测试。
希望这会有所帮助。
谢谢, 阿尼尔班
【讨论】:
以上是关于执行业力测试时出错的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Angular 8 CircleCI 构建在执行 ng test --watch-false 时失败,业力配置为 singleRun=true?
尝试使用 pytest 在 python 3.10 上执行测试时出错