Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带Karma的AngularJS测试控制器_Angularjs_Unit Testing_Karma Runner - Fatal编程技术网

带Karma的AngularJS测试控制器

带Karma的AngularJS测试控制器,angularjs,unit-testing,karma-runner,Angularjs,Unit Testing,Karma Runner,我的控制器是: angular.module('mean').controller('ItemsController', ['$scope', function ($scope) { $scope.contentTemplate = '/views/items/index.html'; $scope.subMenu = [ {name: 'Create Item', location: '/items/create'} ]; }]); 我的测试非常简单: describ

我的控制器是:

angular.module('mean').controller('ItemsController', ['$scope', function ($scope) {
  $scope.contentTemplate = '/views/items/index.html';
  $scope.subMenu = [
    {name: 'Create Item', location: '/items/create'}
  ];
}]);
我的测试非常简单:

  describe('ItemsController', function () {
    var scope;

    beforeEach(module('mean'));

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

    }));


    it('should have sub menu items loaded properly', function () {
      expect(scope.subMenu.length).toBe(1);
    });

  });
我想要的是测试是否有一个子菜单项。相反,我得到的错误是:

PhantomJS 1.9.7(Mac OS X)项控制器应具有子菜单项 正确加载失败类型错误:“未定义”不是函数 (正在评估“$rootScope.new()”)


$rootScope不是被注入的吗?那么为什么它没有定义呢?

您想要以美元符号开头的方法:

scope = $rootScope.$new();
//                 ^
这应该可以解决问题