测试$广播事件事件、测试

2023-09-13 05:05:49 作者:薄情少女❀

我有角的测试问题。我试图测试$广播已成功发射。我有一些奇怪的行为试图用spyOn。如果您有任何建议,请帮助我一直在试图解决这个好几个小时。我的测试是这样的:

 描述(注册器',函数(){  beforeEach(模块('对myApp'));  描述(SignupCtrl',函数(){    变量$范围,CTRL $ httpBackend,AUTH_EVENTS,$ rootScope;    beforeEach(注(函数($喷油器,控制器$,      _ $ httpBackend_,_apiUrl_,_AUTH_EVENTS _){        $ rootScope = $ injector.get('$ rootScope');        $范围= $ rootScope $新的()。        CTRL = $控制器('SignupCtrl',{$范围:$范围});        $ httpBackend = _ $ httpBackend_;        apiUrl = _apiUrl_;        AUTH_EVENTS = _AUTH_EVENTS_;        spyOn($ rootScope,$广播);      }    ));    afterEach(函数(){      $ httpBackend.verifyNoOutstandingExpectation();      $ httpBackend.verifyNoOutstandingRequest();    });    它('应显示来自API的错误消息,函数(){      VAR apiMessage ='该电子邮件已经存在。;      $ httpBackend.expectPOST('/用户)响应(400,{        消息:apiMessage      });      //故作空凭据呼叫控制寄存器功能      $ scope.register({});      $ httpBackend.flush();      预计.toHaveBeenCalledWith(AUTH_EVENTS.signupFailed)($ rootScope $播出。);      期待($ scope.errorMessage).toBe(apiMessage);      期待($ scope.showErrorMessage).toBe(真);    });  });}); 

和我得到的错误是:

 类型错误:未定义是不是(评估'$ rootScope $广播('$ locationChangeStart',$ location.absUrl(),OLDURL)的对象。            默认prevented')  在/src/web/src/vendor-bower/angular/angular.js:9789  在/src/web/src/vendor-bower/angular/angular.js:12595  在/src/web/src/vendor-bower/angular/angular.js:12407  在/src/web/src/vendor-bower/angular-mocks/angular-mocks.js:1438 

解决方案 做核酸检测的注意事项 九张图说清楚了

尝试 spyOn(rootScope,'$广播')andCallThrough(); 键,让我知道如何其道理。也看到我的评论。

I have a problem with angular tests. I'm trying to test that $broadcast was successfully fired. I'm having some strange behavior trying to use spyOn. If you have any suggestions please help I've been trying to solve this for hours. My test looks like this:

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);
    });

  });
});

And the error I'm getting is:

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

解决方案

Try spyOn(rootScope,'$broadcast').andCallThrough(); and let me know how it goes. Also see my comment.