简体   繁体   中英

unit testing directive and isolatedScope not a function

I am trying to unit test a directive that was previously written and for my first unit test all I want to do is check a variable inside the scope of the directive.

However, every time I try to run the method isolatedScope() on my html element, I receive the error elem.isolatedScope is not a function .

One of the strange things is, when I set a breakpoint just after I run $compile on my element, from the javascript console, I am able to print the directives isolated scope.

Trying to set my isolatedScope to a variable like below, will always result in the error 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;
        });
    });
});

Any idea on what I might be doing wrong?

It is isolateScope , not isolatedScope .

Isolate scope and isolated scope terms can be used interchangeably, but the first one is preferred in official sources (it was likely coined by one of the framework authors):

What we want to be able to do is separate the scope inside a directive from the scope outside, and then map the outer scope to a directive's inner scope. We can do this by creating what we call an isolate scope .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM