2015-04-03 74 views
0

我正在測試一個隔離指令,該指令作爲控制器中使用的內部控制器和指令範圍值。從指令的內部控制器訪問值的單元測試設置

這是來自真實代碼的簡化運動員http://plnkr.co/edit/r91xk1?p=preview,它是網格的指令包裝,但是這顯示了問題。從測試

節選是失敗的:

beforeEach(inject(function($rootScope, $controller, $compile) { 
     $compile = $compile; 
     $scope = $rootScope.$new(); 

     //Here is the scope information defined, what we want to get into the directive 
     $scope.itemToTest = "-beforeTest filled in valueFromTest--" 

     // Here we declare that the "itemToTest" attribute is bound to the *value* of itemToTest 
     elm = angular.element('<primary-grid itemToTest="itemToTest" ></primary-grid>'); 
     e = $compile(elm)($scope); 
     $scope.$digest(); 
     console.log("in beforeEach $scope %o ", $scope.$id); 
    })); 


    it('should have filled in the value the grid', function() { 
     // Access the isolateScope using <ELEMENT>.isolateScope, to see what's happening inside 
     //This fails 
     console.log("in Test e.scope() %o ", e.scope()); 
     console.log("in Test e.isolateScope() %o ", e.isolateScope().$id); 
     console.log("in Test e.isolateScope().itemToTest %o ", e.isolateScope().itemToTest); 
     console.log("in Test e %o ", e); 
     //This Test fails 
     expect(e.isolateScope().itemToTest).toEqual($scope.itemToTest); 
    }) 

...

什麼是設置在測試指令的正確值的最佳方式,任何想法,使控制器能夠使用它和代碼可以測試單元?

感謝

回答

0

解決:需要使用 '項目 - 測試' 作爲標記屬性,而不是駝峯(即JS)名稱

使用:

elm = angular.element('<primary-grid item-to-test="itemToTest" ></primary-grid>’); 

代替作者:

elm = angular.element('<primary-grid itemToTest="itemToTest" ></primary-grid>’);