How to test on destroy scope

后端 未结 4 865
刺人心
刺人心 2021-02-13 19:02

How to unit testing an $destroy event of a Directive in angularjs?

I have the code in my directive:

scope.$on(\'$destroy\', function () {
    //clean som         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 19:16

    You should test the code that is executed within the $destroy event. Here's a contrived example using a controller:

    Test

    it('sets destroyed to true when the scope is destroyed', function() {
      // Arrange
      $scope.destroyed = false;
    
      // Act
      $scope.$destroy();
    
      // Assert
      expect($scope.destroyed).toBe(true);
    });
    

    Controller

    app.controller('MainCtrl', function($scope) {
      $scope.$on('$destroy', function() {
        $scope.destroyed = true;
      });
    });
    

    Plunker here.

提交回复
热议问题