简体   繁体   English

单元测试指令和isolatedScope不是函数

[英]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 . 但是,每次我尝试在html元素上运行方法isolatedScope()时,都会收到错误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. 一件奇怪的事是,当我在javascript控制台上对元素运行$compile之后立即设置断点时,我能够打印指令隔离范围。

Trying to set my isolatedScope to a variable like below, will always result in the error elem.isolatedScope is not a function . 尝试将我的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;
        });
    });
});

Any idea on what I might be doing wrong? 对我可能做错的事情有任何想法吗?

It is isolateScope , not isolatedScope . 它是isolateScope ,而不是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 . 我们可以通过创建隔离范围来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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