AngularJS 中的单元测试出错 - 具有许多依赖项的控制器

Error with unit tests in AngularJS - controller with many dependency

我在 AngularJS 中遇到一些单元测试问题。我从 github angular 种子应用程序中获取并编写了一些模块。现在当我尝试测试它时出现错误。 问题仅出现在单元测试中 - 在浏览器应用程序中运行正常。

感觉应该是个容易解决的问题,但是找不到哪里出错了

我的控制器:

'use strict';
angular.module('myApp.student', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/student', {
    templateUrl: 'student.html',
    controller: 'StudentCtrl'
  });
}])
.controller('StudentCtrl', ['$scope', '$http', '$modal', '$log', function($scope, $http, $modal, $log) { ... }]);

我的单元测试dokumenty_test.js:

describe('myApp.student', function() {
    beforeEach(module('myApp.student'));

  describe('StudentCtrl controller', function(){

    it('should ....', inject(function($controller) {
      //spec body
      var view1Ctrl = $controller(StudentCtrl');
      expect(view1Ctrl).toBeDefined();
    }));

  });
});

以及来自控制台的错误: 错误:[$injector:unpr] 未知提供者:$scopeProvider <- $scope http://errors.angularjs.org/1.2.28/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope

提前致谢

您需要在创建控制器时手动设置它的依赖项:

it('should ....', inject(function($controller, $rootScope, $http, $modal, $log) {

  var view1Ctrl = $controller('StudentCtrl', {
    '$scope': $rootScope.$new(),
    '$http': $http,
    '$modal': $modal,
    '$log': $log
  });

  expect(view1Ctrl).toBeDefined();
}));

演示: http://plnkr.co/edit/IciIZvjRu66P3NhDc6ud

请注意,我将模块 ui.bootstrap 添加为应用程序的依赖项,因为控制器正在使用 $modal

您可以阅读有关单元测试控制器的更多信息here