0

我按照http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-karma.html中的教程使用 Karma 测试 AngularJS 应用程序。

我的 karma.conf.js 看起来像这样:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      'public/bower_components/angular/angular.min.js',
      'public/bower_components/angular-route/angular-route.min.js',
      'public/bower_components/d3/d3.js',
      'public/bower_components/nvd3/nv.d3.js',
      'public/bower_components/angularjs-nvd3-directives/dist/angularjs-nvd3-directives.js',
      'public/bower_components/angular-mocks/angular-mocks.js',
      'public/scripts/**/*.js',
      'test/*Spec.js'
    ],
    exclude: [
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

我的测试:

describe("app", function() {

  beforeEach(angular.mock.module('app'));

  it('should have a HomeController', function() {
    expect(app.HomeController).not.to.equal(null);
  });

});

公共/脚本/app.js

angular.module('app', [
  'ngRoute',
  'app.controllers',
  'nvd3ChartDirectives'
]);

我在控制台中得到一个输出说:应用程序应该有一个 HomeController FAILED ReferenceError: app is not defined

任何帮助,将不胜感激,

提前致谢!

4

1 回答 1

0

解决了:

describe('app', function(){
  beforeEach(module('app'));

  var ctrl, scope;
  beforeEach(inject(function($controller, $rootScope) {
    scope = $rootScope.$new();
    ctrl = $controller('HomeController', {
      $scope: scope
    });
  }));

  it('should have defined HomeController', function() {
    expect(ctrl).toBeDefined();
  });
}
于 2015-07-08T21:34:44.223 回答