执行业力测试时出错

时间:2014-05-23 00:46:13

标签: angularjs unit-testing gruntjs jasmine karma-runner

我正在尝试为我的应用程序运行karma单元测试。该应用程序是使用我公司的定制角度yeoman发生器创建的。 (发电机非常类似于角度 - 自动发电机)。

我正在尝试为我的应用程序运行业力茉莉花测试。但是每当我运行 grunt test

时,我都会继续收到错误消息
  

Chrome 35.0.1916(Mac OS X 10.9.0)错误未捕获对象   /用户//虚拟   应用程序/ karma1 /应用程序/ bower_components /角度/ angular.js:1611

     

警告:任务"业力:单位"失败。使用--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仅在指令中使用。作为一个开始,我想测试我的控制器和服务,但我不断收到此错误。

知道出了什么问题。

谢谢!

2 个答案:

答案 0 :(得分:1)

我错过了一点,我没有加载角应用程序。我正在通过配置文件config.js

加载角度应用程序
  

/ * global 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'         })         。除此以外({           redirectTo:' / site'         }); }]);

在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对我的应用程序进行单元测试。

希望这有帮助。

谢谢, Anirban

答案 1 :(得分:0)

对于遇到错误的其他人,要获取有关抛出异常的更多信息,您需要在您的karma配置中使用其他浏览器,因为Chrome目前存在一个已知问题,它不会显示完整的错误消息。这有助于您了解设置的问题。有关详细信息,请参阅angular.js问题5568:https://github.com/angular/angular.js/issues/5568