2016-12-22 14 views
0

我想單元測試以前編寫的指令,對於我的第一個單元測試,我想要做的就是檢查指令範圍內的變量。單元測試指令和isolatedScope不是函數

但是,每當我嘗試在我的html元素上運行方法isolatedScope()時,我收到錯誤elem.isolatedScope is not a function

其中一個奇怪的事情是,當我在我的元素上運行$compile後,在javascript控制檯上設置了一個斷點後,我就可以打印指令隔離範圍。

試圖將我的isolatedScope設置爲像下面這樣的變量,將始終導致錯誤elem.isolatedScope is not a function

describe('directive: wikis', function() { 
    var $scope, elem, isolated; 

    beforeEach(module('app')); 

    beforeEach(inject(function(_$compile_, _$rootScope_){ 
     $scope = _$rootScope_.$new(); 

     elem = angular.element('<wikis></wikis>'); 
     elem = _$compile_(elem)($scope); 
     isolated = elem.isolatedScope() 
     isolated.$digest(); 
    })); 

    describe('$scope should be defined', function(){ 
     it('data should be empty object', function() { 
      expect(isolated.data).to.be.null; 
     }); 
    }); 
}); 

任何想法,我可能會做錯什麼?

+0

isolateScope已被棄用(角1.3?),所以它不會工作,除非你使用的是早期版本。 – rrd

回答

1

It is isolateScope而不是isolatedScope

隔離範圍隔離範圍術語可以互換使用,但第一個is preferred in official sources(它可能是由框架作者之一杜撰):

我們希望能夠給什麼do將指示 中的作用域與外部作用域分開,然後將外部作用域映射到指令的內部作用域 。我們可以通過創建我們所稱的隔離區 範圍來實現。

+0

謝謝。這對我來說長久以來一直是個問題。 –