我正在尝试为我的应用程序运行karma单元测试 . 该应用程序是使用我公司的定制角度yeoman发生器创建的 . (发电机非常类似于角度 - 自动发电机) .
我正在尝试为我的应用程序运行业力茉莉花测试 . 但每当我运行 grunt test 时,我都会继续收到错误消息
Chrome 35.0.1916(Mac OS X 10.9.0)错误/用户//未使用的对象//虚拟应用程序/ 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"
}
}
我的测试控制器是:'use strict';
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仅在指令中使用 . 作为一个开始,我想测试我的控制器和服务,但我不断收到此错误 .
不知道出了什么问题 .
谢谢!
2 回答
我错过了一点,我没有加载角应用程序 . 我正在通过配置文件config.js加载角度应用程序
在karma.conf.js中,我添加了以下脚本标记:
test-main.js只有以下代码行:
在karma.conf.js文件中加载test-main.js文件后,我能够使用karma对我的应用程序进行单元测试 .
希望这可以帮助 .
谢谢,Anirban
对于遇到错误的其他人,要获得有关抛出异常的更多信息,您需要在您的业力配置中使用其他浏览器,因为Chrome目前存在一个已知问题,它不会显示完整的错误消息 . 这有助于您了解设置的问题 . 有关更多信息,请参见angular.js问题5568:https://github.com/angular/angular.js/issues/5568