测试元素指令 - 在测试期间无法访问隔离的范围方法

leo*_*nfs 24 angularjs angularjs-directive angularjs-scope

我有以下指令.

directivesModule.directive('wikis', function() {
var urlRegex = new RegExp('^(https?)://.+$');

return {
    restrict: 'E', 
    templateUrl: 'templates/wiki-list.html',
    scope: {
        wikis: '='
    },
    link: function(scope, element, attrs) {
        scope.newWikiURL = '';

        scope.$watch('wikis', function() {
            if (scope.wikis == undefined || scope.wikis.length === 0) {
                scope.class = 'hide';
            } else {
                scope.class = 'show';
            }
        }, false);

        scope.addWiki = function() {
            if (scope.wikis == undefined) {
                scope.wikis = [];
            }

            var nw = scope.newWikiURL;
            if (nw != undefined && nw.trim() != '' && urlRegex.exec(nw)) { 
                scope.wikis.push(nw);
                scope.newWikiURL = '';
            }
        }

    }
};

});
Run Code Online (Sandbox Code Playgroud)

当我测试它时:

describe('Wikis Directive Test Suite', function() {
    var scope, elem, directive, linkFn, html;

    beforeEach(function() {
        html = '<wikis wikis=''></wikis>';

        inject(function($compile, $rootScope) {
            scope = $rootScope.$new();
            scope.wikis = [];

            elem = angular.element(html);

            $compile(elem)(scope);

            scope.$digest();
        });

    });

    it('add Wiki should add a valid wiki URL to artist', function() {
        var url = 'http://www.foo.com';
        scope.newWikiURL = url;
        scope.addWiki();

        expect(scope.wikis.length).toBe(1);
        expect(scope.wikis[0]).toBe(url);
        expect(scope.newWikiURL).toBe('');
    });
});
Run Code Online (Sandbox Code Playgroud)

我得到一个错误,说Object没有addWiki方法.我尝试调试它,并且在测试期间不调用链接函数.我怀疑这就是为什么addWiki方法不是范围的一部分.谁知道我为什么会收到这个错误?

顺便说一句,将一些逻辑添加到指令的链接函数中是一种常规做法,因为它本身就是一个控制器本身吗?因为查看代码我知道这就是为什么我实际上在做什么.

yer*_*ero 40

根据角度1.2.0文档,获取隔离范围的方法是通过isolateScope方法

  • scope() - 检索当前元素或其父元素的范围.

  • isolateScope() - 如果一个直接连接到当前元素,则检索隔离范围.此getter应仅用于包含启动新隔离范围的指令的元素.在此元素上调用scope()始终返回原始的非隔离范围.

Angular doc - section jQuery/jqLit​​e extras

BREAKING CHANGE:jqLit​​e#scope()

  • 谢谢,我几个小时都在苦苦挣扎.这是Angular 1.2及以上版本的正确答案. (3认同)

noj*_*noj 39

  1. 你需要加载包含您的指令模块,否则角不知道什么<wikis>

  2. 您的指令创建了一个隔离范围,因此一旦编译完成,您需要使用新的范围 elem.isolateScope()

所以有了这些变化:

describe('Wikis Directive Test Suite', function() {
    var $scope, scope, elem, directive, linkFn, html;

    beforeEach(module('app'));

    beforeEach(function() {
        html = '<wikis></wikis>';

        inject(function($compile, $rootScope, $templateCache) {
            $templateCache.put('templates/wiki-list.html', '<div>wiki template</div>');

            $scope = $rootScope.$new();
            $scope.wikis = [];

            elem = angular.element(html);

            $compile(elem)($scope);

            scope = elem.isolateScope();
            scope.$apply();
        });

    });

    it('add Wiki should add a valid wiki URL to artist', function() {
        var url = 'http://www.foo.com';
        scope.newWikiURL = url;
        scope.addWiki();

        expect(scope.wikis.length).toBe(1);
        expect(scope.wikis[0]).toBe(url);
        expect(scope.newWikiURL).toBe('');
    });
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/QGmCF/1/