Karma:ReferenceError:app未定义

时间:2014-08-25 13:00:01

标签: angularjs unit-testing karma-runner karma-mocha

我按照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'
]);

我在控制台中得到一个输出:app应该有一个HomeController FAILED     ReferenceError:app未定义

任何帮助将不胜感激,

提前致谢!

1 个答案:

答案 0 :(得分: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();
  });
}