Angularjs $scope.$on不在Jasmine SpecRunner中工作

Angularjs $scope.$on不在Jasmine SpecRunner中工作,angularjs,jasmine,Angularjs,Jasmine,我刚刚开始使用Jasmine测试AngularJS应用程序。 我相信我正在SpecRunner上执行所有导入操作,但我得到了一个错误: TypeError: $scope.$on is not a function 我在我的控制器中使用它,如下所示: app.controller('asstCtrl', function($scope, $uibModal, $interval, $document) { $scope.$on('$includeContentLoaded', func

我刚刚开始使用Jasmine测试AngularJS应用程序。 我相信我正在SpecRunner上执行所有导入操作,但我得到了一个错误:

TypeError: $scope.$on is not a function
我在我的控制器中使用它,如下所示:

app.controller('asstCtrl', function($scope, $uibModal, $interval, $document) {
    $scope.$on('$includeContentLoaded', function() {        
        $scope.doStuff();          
    });
});

...
我的SpecRunner测试如下所示:

describe('calculator', function(){
   beforeEach(module('asst'));
   it('basic', inject(function($controller) {
      var scope = {},
          ctrl = $controller('asstCtrl', {$scope:scope});
     expect(scope.x).toBe(1);
   }));
});
describe('calculator', function(){
   beforeEach(module('asst'));
   var rootScope;
   beforeEach(inject(function($rootScope) {
     rootScope = $rootScope;
   }));
   it('basic', inject(function($controller) {
      var scope = rootScope.$new(),
          ctrl = $controller('asstCtrl', {$scope:scope});
     expect(scope.x).toBe(1);
   }));
});
以下是我的作品:

  • public/js/jasmine-2.4.0/jasmine.js
  • public/js/jasmine-2.4.0/jasmine-html.js
  • public/js/jasmine-2.4.0/boot.js
  • public/js/jquery.min.js
  • public/js/angular.js public/js/angular-animate.min.js
  • public/js/angular-mocks.js
  • public/js/ui-bootstrap-tpls-0.14.2.min.js
如果我从控制器中删除$scope.$on部分,应用程序将正常运行,测试将运行。当我注入控制器时,我一定是在SpecRunner中做了一些错误的事情,或者某些事情的工作方式不同。有人能帮我吗

编辑

测试结果如下:

describe('calculator', function(){
   beforeEach(module('asst'));
   it('basic', inject(function($controller) {
      var scope = {},
          ctrl = $controller('asstCtrl', {$scope:scope});
     expect(scope.x).toBe(1);
   }));
});
describe('calculator', function(){
   beforeEach(module('asst'));
   var rootScope;
   beforeEach(inject(function($rootScope) {
     rootScope = $rootScope;
   }));
   it('basic', inject(function($controller) {
      var scope = rootScope.$new(),
          ctrl = $controller('asstCtrl', {$scope:scope});
     expect(scope.x).toBe(1);
   }));
});

您正在创建一个对象并将其作为控制器的作用域注入。在该对象上不会有
$on
方法

而是注入
$rootScope
并使用
$new
方法创建范围:

var scope = $rootScope.$new(),
    ctrl = $controller('asstCtrl', { $scope: scope });