2013-08-06 138 views
1

指令這是我的指令單元測試角JS使用依賴

.directive('xDirective', ['x', 'y', function (x, y) { 
     return { 
      restrict: 'CA', 
      link: function (scope, element, attrs, messaging) { 
       //console.log(scope); 
       //console.log(element); 
       //console.log(attrs); 

       if (x.isResponsive && x.responsiveMode === y.responsive.mode.phone) { 

        //set the height of the header based on the device height 
        var offset = (attrs.heightOffset ? attrs.heightOffset : 0);      
        element.css("height", (x.device.height - offset) + 60 + "px"); 
       } 
      } 
     }; 
    }]) 

其中x和y是依賴於directive..i想單元測試這個directive..how做我繼續用茉莉花。

謝謝。

回答

3

我計算出來使用下述計算器螺紋

Testing angularjs directive with dependencies

下面是代碼:

describe('Directive: AppVersion', function() { 
    beforeEach(module('MyApp')); 

    var element; 

    it('should have element text set to config value', inject(function ($rootScope, $compile, x,y) { 
     var scope = $rootScope; 
     element = $compile('<div class="someClass" xDirective >some Content</div>')(scope); 
     expect($(element).find('.someClass').css('height')).toBe(config.version); 
    })); 
}); 

在它塊內即時注射 'X' 和 'y' 模塊這是對指令的依賴關係

謝謝