Javascript 使用Karma进行角度测试:注入控制器后,$controller()未定义

Javascript 使用Karma进行角度测试:注入控制器后,$controller()未定义,javascript,angularjs,unit-testing,jasmine,karma-runner,Javascript,Angularjs,Unit Testing,Jasmine,Karma Runner,我正试着为《因果报应》和《茉莉花》做测试。我已经成功地安装和配置了Karma,但是我在使用角度模拟时遇到了问题。在下面的aTest.spec.js中,我提供了一个简单的应用程序、控制器和测试规范来说明这个问题。谁能告诉我我做错了什么 来自Karma的我的控制台输出: Chrome 39.0.2171 (Mac OS X 10.8.5) ControllerForTest encountered a declaration exception FAILED TypeError: undefined

我正试着为《因果报应》和《茉莉花》做测试。我已经成功地安装和配置了Karma,但是我在使用角度模拟时遇到了问题。在下面的aTest.spec.js中,我提供了一个简单的应用程序、控制器和测试规范来说明这个问题。谁能告诉我我做错了什么

来自Karma的我的控制台输出:

Chrome 39.0.2171 (Mac OS X 10.8.5) ControllerForTest encountered a declaration exception FAILED
TypeError: undefined is not a function
    at Suite.<anonymous> (/Users/duncanmalashock/python_projects/scout/public/tests/unit/aTest.spec.js:19:20)
    at jasmineInterface.describe (/Users/duncanmalashock/python_projects/scout/node_modules/karma-jasmine/lib/boot.js:59:18)
    at /Users/duncanmalashock/python_projects/scout/public/tests/unit/aTest.spec.js:13:1
...
files: [
  'http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js',
  'angular/vendor/angular-mocks.js',
  'tests/unit/*.spec.js'
],
...
var testApp = angular.module('testApp', []);

testApp.controller('ControllerForTest', ['$scope',
  function($scope) {
    $scope.data = {
      a: 'foo',
      b: 'bar',
      c: 'baz'
    };
  }
]);
describe('ControllerForTest', function() {
  module('testApp');
  var $controller;
  inject(function(_$controller_) {
      $controller = _$controller_;
  });
  var controller = $controller('ControllerForTest');
  it('is defined', function() {
    expect($controller).toBeDefined();
  });
});
控制器:

Chrome 39.0.2171 (Mac OS X 10.8.5) ControllerForTest encountered a declaration exception FAILED
TypeError: undefined is not a function
    at Suite.<anonymous> (/Users/duncanmalashock/python_projects/scout/public/tests/unit/aTest.spec.js:19:20)
    at jasmineInterface.describe (/Users/duncanmalashock/python_projects/scout/node_modules/karma-jasmine/lib/boot.js:59:18)
    at /Users/duncanmalashock/python_projects/scout/public/tests/unit/aTest.spec.js:13:1
...
files: [
  'http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js',
  'angular/vendor/angular-mocks.js',
  'tests/unit/*.spec.js'
],
...
var testApp = angular.module('testApp', []);

testApp.controller('ControllerForTest', ['$scope',
  function($scope) {
    $scope.data = {
      a: 'foo',
      b: 'bar',
      c: 'baz'
    };
  }
]);
describe('ControllerForTest', function() {
  module('testApp');
  var $controller;
  inject(function(_$controller_) {
      $controller = _$controller_;
  });
  var controller = $controller('ControllerForTest');
  it('is defined', function() {
    expect($controller).toBeDefined();
  });
});
aTest.spec.js:

Chrome 39.0.2171 (Mac OS X 10.8.5) ControllerForTest encountered a declaration exception FAILED
TypeError: undefined is not a function
    at Suite.<anonymous> (/Users/duncanmalashock/python_projects/scout/public/tests/unit/aTest.spec.js:19:20)
    at jasmineInterface.describe (/Users/duncanmalashock/python_projects/scout/node_modules/karma-jasmine/lib/boot.js:59:18)
    at /Users/duncanmalashock/python_projects/scout/public/tests/unit/aTest.spec.js:13:1
...
files: [
  'http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js',
  'angular/vendor/angular-mocks.js',
  'tests/unit/*.spec.js'
],
...
var testApp = angular.module('testApp', []);

testApp.controller('ControllerForTest', ['$scope',
  function($scope) {
    $scope.data = {
      a: 'foo',
      b: 'bar',
      c: 'baz'
    };
  }
]);
describe('ControllerForTest', function() {
  module('testApp');
  var $controller;
  inject(function(_$controller_) {
      $controller = _$controller_;
  });
  var controller = $controller('ControllerForTest');
  it('is defined', function() {
    expect($controller).toBeDefined();
  });
});

模块实例化和服务注入应该在每个之前的
中进行,而不是直接在
描述
块中进行。这将使它们可用于以下每个
it
s

您也不需要测试
$controller
,这是一种角度服务。改为测试您的控制器

describe('ControllerForTest', function() {
  var $controller;
  var ControllerForTest;

  beforeEach(function() {
    module('testApp');

    inject(function(_$controller_) {
        $controller = _$controller_;
    });
  });

  it('is defined', function() {
    // This line can also be in the beforeEach.
    // Saves having to repetitively instantiate the controller.
    ControllerForTest = $controller('ControllerForTest');

    expect(ControllerForTest).toBeDefined();
  });
});