2015-05-15 10 views
10

Tôi đoán tôi gặp sự cố di chuyển với angular-animate.js từ phiên bản 1.2 đến 1.3. Dưới đây là hình ảnh động của tôiLàm thế nào để hoạt hình thử nghiệm đồng bộ trong AngularJS 1.3.15?

'use strict'; 
angular.module('cookbook', ['ngAnimate']) 
    .animation('.slide-down', function() { 
     var NG_HIDE_CLASS = 'ng-hide'; 

     return { 
      beforeAddClass: function(element, className, done) { 
       alert('before add'); 
       if(className === NG_HIDE_CLASS) { 
        element.slideUp(done); 
       } 
      }, 
      removeClass: function(element, className, done) { 
       if(className === NG_HIDE_CLASS) { 
        element.hide().slideDown(done); 
       } 
      } 
     }; 
    }); 

Synchronous kiểm tra

'use strict'; 

describe('A Brief Look At Testing Animations(changed) - ', function() { 
    var scope; 
    var element; 
    var $animate; 
    var $rootElement; 

    beforeEach(module('cookbook', 'ngAnimateMock')); 

    describe('Synchronous testing of animations', function() { 

     var animatedShow = false; 
     var animatedHide = false; 

     beforeEach(module(function($animateProvider) { 
      $animateProvider.register('.slide-down', function() { 
       return { 
        beforeAddClass: function(element, className, done) { 
         debugger;alert(1); 
         animatedHide = true; 
         done(); 
        }, 
        removeClass: function(element, className, done) { 
         animatedShow = true; 
         done(); 
        } 
       }; 
      }); 
     })); 

     beforeEach(inject(function($injector) { 
      scope = $injector.get('$rootScope').$new(); 
      $rootElement = $injector.get('$rootElement'); 
     })); 

     beforeEach(inject(function($compile) { 
      element = angular.element('<div class="slide-down" ng-show="hint"></div>'); 
      $compile(element)(scope); 
      scope.$digest(); 
      $rootElement.append(element); 
     })); 

     it('should animate to show', function() { 
      scope.hint = true; 
      scope.$digest(); 
      expect(animatedShow).toBeTruthy(); 
     }); 

     it('should animate to hide', function() { 
      scope.hint = true; 
      scope.$digest(); 
      scope.hint = false; 
      scope.$digest(); 
      expect(animatedHide).toBeTruthy(); 
     }); 

    }); 
}); 

và spec Á hậu

<!DOCTYPE HTML> 
<html> 

<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Angular Spec Runner</title> 

    <link rel="shortcut icon" type="image/png" href="../../lib/jasmine-2.0.0/jasmine_favicon.png"> 
    <link rel="stylesheet" type="text/css" href="../../lib/jasmine-2.0.0/jasmine.css"> 

    <script type="text/javascript" src="../../lib/jasmine-2.0.0/jasmine.js"></script> 
    <script type="text/javascript" src="../../lib/jasmine-2.0.0/jasmine-html.js"></script> 
    <script type="text/javascript" src="../../lib/jasmine-2.0.0/boot.js"></script> 

    <script type="text/javascript" src="../../lib/angular-1.2.28_/jquery-1.11.1.min.js"></script> 
    <script type="text/javascript" src="../../lib/angular-1.3.15/angular.js"></script> 
    <script type="text/javascript" src="../../lib/angular-1.3.15/angular-route.js"></script> 
    <script type="text/javascript" src="../../lib/angular-1.3.15/angular-ui-router.js"></script> 
    <script type="text/javascript" src="../../lib/angular-1.3.15/angular-mocks.js"></script> 
    <script type="text/javascript" src="../../lib/angular-1.2.28_/angular-animate.js"></script> 

    <!--DOESN'T WORK WITH 1.3.15--> 
    <!--<script type="text/javascript" src="../../lib/angular-1.3.15/angular-animate.js"></script>--> 


    <!-- include source files here... --> 
    <script type="text/javascript" src="../src/cookbook.js"></script> 
    <link rel="stylesheet" href="../src/styles.css"> 

    <!-- include spec files here... --> 
    <script type="text/javascript" src="cookbookSpec.js"></script> 

</head> 

<body> 
</body> 

</html> 

Khi tôi sử dụng góc-Animate 1.2.28 tất cả các bài kiểm tra được thông qua nhưng sau khi chuyển sang 1.3. 15 kiểm tra không thành công. Bây giờ, tôi đang cố gắng để điều tra sự khác biệt giữa hai phiên bản của angular-animate. Có lẽ, ai đó đã gặp rắc rối này. Cảm ơn tất cả các câu trả lời của bạn.

+0

Kiểm tra không thành công như thế nào? Cảm ơn. – alecxe

+0

@alecxe với thông báo này 'Mong đợi sai sự thật.' – BILL

+0

Tôi không hoàn toàn chắc chắn như thế nào ngay cả những thử nghiệm ban đầu (khi nó làm việc) thử nghiệm các hình ảnh động thực sự. Dường như nó sẽ vượt qua ngay cả khi không có mã hoạt hình thực, và vì vậy chỉ kiểm tra mã trong bản thân bài kiểm tra, điều này có vẻ hơi vô nghĩa! (Tất nhiên nếu tôi sai, hãy sửa tôi ...) –

Trả lời

2

Trong khi cố gắng để đưa các thử nghiệm để làm việc tôi nhận ra rằng tôi chỉ có thể:

  • addClass sử dụng và removeClass từ $ động để Add/Remove ng-hide lớp; sử dụng "ng-show" trực tiếp không hoạt động. Khi sử dụng các phương pháp đó tôi nhận ra rằng phương pháp "removeClass" từ đối tượng đăng ký là phim hoạt hình chưa từng được gọi, những cái mà được gọi là: "beforeAddClass" và "beforeRemoveClass", vì vậy tôi đã thay đổi mà . Khi tôi google về nó tôi thấy một vấn đề liên quan phần nào: $animate.removeClass() doesn't work if addClass animation incomplete.
  • sử dụng rootScope thay vì phạm vi (Tôi không thể hiểu tại sao)

Sau khi tìm kiếm nhiều hơn một chút tôi thấy comment này về một vấn đề; có vẻ như có một số lỗi với hoạt ảnh; nhận xét gợi ý để sử dụng các phiên bản của differente gócgóc-động như có "một vài sửa lỗi cho animate trong các đường ống". Vì vậy, tôi dùng thử và sử dụng các phiên bản cập nhật (v1.4.0-build.4010 + sha.213c2a7) và bây giờ nó có vẻ hoạt động tốt hơn ... Sử dụng chỉ thị ng-show hiện hoạt động. (nhưng vẫn chỉ beforeAddClass và beforeRemoveClass được gọi và không thể nhận phạm vi hoạt động ...).

Tôi không đề xuất bạn nâng cấp (ngay cả vì v.1.4.0 chưa được phát hành) chỉ trỏ rằng có vấn đề và lỗi xung quanh chủ đề này ...) Đây là mã (với các phiên bản được đề xuất v1.4.0-build.4010):

describe('animate', function() { 
    var scope, $animate, $rootElement, $compile, $rootscope; 

    describe('Synchronous testing of animations', function() { 

    var animatedShow = false; 
    var animatedHide = false; 

    beforeEach(module('cookbook', function($animateProvider) { 
     animatedShow = false; 
     animatedHide = false; 

     $animateProvider.register('.slide-down', function() { 
     return { 
      beforeAddClass: function(element, className, done) { 
      animatedHide = true; 
      done(); 
      }, 
      beforeRemoveClass: function(element, className, done) { 
      animatedShow = true; 
      done(); 
      } 
     }; 
     }); 
    })); 
    beforeEach(inject(function(_$injector_, _$animate_, _$compile_) { 
     $compile = _$compile_; 
     $animate = _$animate_; 
     $rootscope = _$injector_.get('$rootScope'); 
     scope = _$injector_.get('$rootScope').$new(); 
     $rootElement = _$injector_.get('$rootElement'); 
    })); 
    it('should animate to hide', function() { 
     $rootscope.hint = true; 
     var el = $compile('<div class="slide-down" ng-show="hint"></div>')($rootscope); 
     $rootElement.append(el); 
     angular.element(document.body).append($rootElement); 
     $rootscope.$digest(); 
     $rootscope.hint = false; 
     $rootscope.$digest(); 
     expect(animatedHide).toBeTruthy(); 
    }); 
    it('should animate to show', function() { 
     $rootscope.hint = false; 
     var el = $compile('<div class="slide-down" ng-show="hint"></div>')($rootscope); 
     $rootElement.append(el); 
     angular.element(document.body).append($rootElement); 
     $rootscope.$digest(); 
     $rootscope.hint = true; 
     $rootscope.$digest(); 
     expect(animatedShow).toBeTruthy(); 
    }); 
    }); 
}); 

tôi cũng đồng ý rằng thử nghiệm này không được thử nghiệm mã hình ảnh động sản; có lẽ một bài kiểm tra e2e nên tốt hơn.

Đây là đoạn mã sử dụng góc cạnh và góc-Animate v1.3.15 và sử dụng addClassremoveClass trực tiếp:

//...the rest of code is identical 

    it('should animate to hide', function() { 
     var el = $compile('<div class="slide-down"></div>')($rootscope); 
     $rootElement.append(el); 
     angular.element(document.body).append($rootElement); 
     $rootscope.$digest(); 
     $animate.addClass(el, 'ng-hide'); 
     $rootscope.$digest(); 
     expect(animatedHide).toBeTruthy(); 
    }); 
    it('should animate to show', function() { 
     var el = $compile('<div class="slide-down ng-hide"></div>')($rootscope); 
     $rootElement.append(el); 
     angular.element(document.body).append($rootElement); 
     $rootscope.$digest(); 
     $animate.removeClass(el, 'ng-hide'); 
     $rootscope.$digest(); 
     expect(animatedShow).toBeTruthy(); 
    }); 
    //... 

Tôi hy vọng điều này sẽ giúp ... Cảm ơn.

Các vấn đề liên quan