IsolateScope () returns undefined when testing the angular directive

Using Angular v1.2.25 and the rails resource pipeline, I am trying to verify that the directive allocation area is indeed updated. Since isolateScope () returns undefined, I expect undefined to be defined ... '

describe("cool directive", function() { beforeEach(module('necessaryModule')); var scope, $rootScope, $compile, elem, baseElement = '<div auto="mock_a" inc="mock_p" method="mock_m" reset-method="mock_r"></div>'; beforeEach(inject(function( _$rootScope_, _$compile_, _$httpBackend_, $http){ $compile = _$compile_; $rootScope = _$rootScope_; scope = $rootScope.$new(); angular.extend(scope, { mock_a: [ {name: "example1"}, {name: "example2"} ], mock_m: function(){ return $http.get('/mockBackend', { params:{ page: scope.mockPage } }); }, mock_r: function() { scope.page = 1; scope.list = []; load(); }, mock_p: 1 }); $httpListGet = _$httpBackend_; $httpListGet.whenPOST('/api/something').respond({}); $httpListGet.whenGET('/mockBackend').respond({name: "example3"}); $httpListGet.whenGET('/mockBackend?page=1').respond({name: "example3"}); $httpListGet.whenGET('/mockBackend?page=2').respond({name: "example4"}); })); var create = function() { elem = angular.element(baseElement); compiledElement = $compile(elem)(scope); elem.scope().$apply(); return compiledElement; }; it("has 'list' defined", function() { var compiledElem = create(); var isolateElemScope = compiledElem.isolateScope(); $rootScope.$apply(); console.log('isolateElemScope',isolateElemScope); expect(isolateElemScope.list).toBeDefined(); }); 

I expect the scope of directives to be accessible and verified, but I get undefined when I test it. Thanks.

+7
angularjs angularjs-directive jasmine
source share
2 answers

to get isolateScope, I use the following code

 compiledElem.children().scope() 

This is due to the fact that most directives do not use replace , which means that the directive tag is on the page, and the directive implementation added as children of this tag. In this case, the isolation area will belong to the children.

Even if it is not, the fragment should work, as the children will share the parent area.

The only case where this does not work is an extreme scenario where you have 2 nested directives where the internal one uses replace. but I have never seen this.

+12
source share

Other people who came to this page may have added the test to jasmine, but forgot to add a link to their directive.

0
source share

All Articles