Angularjs 测试$broadcast事件

Angularjs 测试$broadcast事件,angularjs,jasmine,Angularjs,Jasmine,我对角度测试有问题。我正在测试$broadcast是否已成功启动。我尝试使用间谍时有一些奇怪的行为。如果你有任何建议,请帮助我,我已经试着解决这个问题好几个小时了。我的测试如下所示: describe('Signup controller', function() { beforeEach(module('myApp')); describe('SignupCtrl', function(){ var $scope, ctrl, $httpBackend, AUTH_EVEN

我对角度测试有问题。我正在测试$broadcast是否已成功启动。我尝试使用间谍时有一些奇怪的行为。如果你有任何建议,请帮助我,我已经试着解决这个问题好几个小时了。我的测试如下所示:

describe('Signup controller', function() {

  beforeEach(module('myApp'));

  describe('SignupCtrl', function(){
    var $scope, ctrl, $httpBackend, AUTH_EVENTS, $rootScope;

    beforeEach(inject(function($injector, $controller,
      _$httpBackend_, _apiUrl_, _AUTH_EVENTS_){

        $rootScope = $injector.get('$rootScope');
        $scope = $rootScope.$new();
        ctrl = $controller('SignupCtrl', {$scope: $scope});
        $httpBackend = _$httpBackend_;
        apiUrl = _apiUrl_;
        AUTH_EVENTS = _AUTH_EVENTS_;
        spyOn($rootScope, "$broadcast");
      }
    ));

    afterEach(function() {
      $httpBackend.verifyNoOutstandingExpectation();
      $httpBackend.verifyNoOutstandingRequest();
    });

    it('should show error message from API', function() {
      var apiMessage = 'That email already exists.';
      $httpBackend.expectPOST('/users').respond(400, {
        message: apiMessage
      });

      // call controller register function with mock empty credentials
      $scope.register({});

      $httpBackend.flush();
      expect($rootScope.$broadcast).toHaveBeenCalledWith(AUTH_EVENTS.signupFailed);
      expect($scope.errorMessage).toBe(apiMessage);
      expect($scope.showErrorMessage).toBe(true);
    });

  });
});
我得到的错误是:

TypeError: 'undefined' is not an object (evaluating '$rootScope.$broadcast('$locationChangeStart', $location.absUrl(), oldUrl).
            defaultPrevented')
  at /src/web/src/vendor-bower/angular/angular.js:9789
  at /src/web/src/vendor-bower/angular/angular.js:12595
  at /src/web/src/vendor-bower/angular/angular.js:12407
  at /src/web/src/vendor-bower/angular-mocks/angular-mocks.js:1438

尝试
spyOn(rootScope,$broadcast')。和callthrough()并让我知道进展如何。另请参阅我的评论。

我认为这是相关的:它实际上起了作用。谢谢Alex这让我烦了一段时间。Jamsine2.0 It和.callThrough()中只有一个注释;谢谢,这些问题让我很恼火。它在迁移到ui路由器后突然出现。